vuejs2项目中moment.js的全局语言环境

时间:2017-08-25 10:16:18

标签: vuejs2 momentjs

目前,我在每个组件中设置了每个创建的钩子中的语言环境:

...
created () {
  moment.locale('nl')
}
...

这可以在一个规则适用于所有组件的地方进行吗?

我正在使用vue webpack模板。

3 个答案:

答案 0 :(得分:4)

您可能想要使用Object.definePrototype。将下面的代码插入您的条目js文件(webpack模板中的src/main.js)。

import moment from 'moment';
// ...
moment.locale('nl');
Object.definePrototype(Vue.prototype, '$moment', { value: moment });

然后,您可以在每个组件中使用this.$moment()时刻 这个解决方案是introduced by Anthony Gore,我确实认为它完全符合您的要求。

答案 1 :(得分:4)

在VUE 2.5中可以这样做:

# Placeholder
x = tf.placeholder(dtype=tf.float32, shape=[None, n_features])
y = tf.placeholder(dtype=tf.float32)

def neural_network_model(data):
    hidden_1_layer = {'weights': tf.Variable(tf.random_normal([4, n_nodes_hl1])),
                      'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))}

    hidden_2_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                  'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))}

    hidden_3_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
              'biases': tf.Variable(tf.random_normal([n_nodes_hl3]))}

    output_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
          'biases': tf.Variable(tf.random_normal([n_classes]))}

    l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases'])
    l1 = tf.nn.relu(l1)

    l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases'])
    l2 = tf.nn.relu(l2)

    l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']), hidden_3_layer['biases'])
    l3 = tf.nn.relu(l3)
    output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']

    return output

def train_neural_network(x):
    import pdb;
    pdb.set_trace()
    prediction = neural_network_model(x)
    cost = tf.reduce_mean(tf.squared_difference(prediction, y))
    optimizer = tf.train.AdamOptimizer().minimize(cost)

    hm_epochs = 5
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        # Training the data
        for epoch in range(hm_epochs):
            epoch_loss = 0
            for i in range(0, len(y_train) // batch_size):
                epoch_x, epoch_y = next_batch(batch_size, x_train, y_train )
                _, c = sess.run([optimizer, cost], feed_dict = {x: epoch_x, y: epoch_y.reshape(-1,1)})
                epoch_loss += c
                print ('Completed %d'%(i))
            print('Epoch', epoch, 'Completed out of', hm_epochs, 'loss:', epoch_loss)   

        #testing the data
        correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))
        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
        print ('Accuracy:', accuracy.eval({x:x_test, y:y_test.reshape(-1,1)}))
train_neural_network(x)

答案 2 :(得分:1)

...
created () {
  this.$root.$moment.locale("nl");
}
...