如何在 Vue.js 2.0 中设置全局分隔符?

How to set global delimiters in Vue.js 2.0?

在Vue.js 1.0中,我可以通过以下代码设置全局分隔符

Vue.config.delimiters = ['${', '}'];

但它已从 Vue.js 2.0 中删除。 每次都必须使用下面的代码来设置分隔符吗?

new Vue({
  delimiters: ['${', '}']
})

据我所知,无法在全局范围内设置分隔符,原因如下:

...in 2.0 delimiters will become a component-level option, which means you only need to set it for the root instance that relies on in-DOM templates. Any components processed by vueify or vue-loader can just keep using default delimiters.

The change is intended to make it easier to use 3rd party components, since changing the delimiters globally means you will not be able to compile them correctly.

来源:https://github.com/vuejs/vue-cli/issues/100

但是简单的解决方法呢?您可以准备 class、const 或任何类型的配置,例如:

VueConfig.js

export const VueConfig = { delimiters: ['${', '}'] };

然后在你的 App.js 上你只需做

```

import {VueConfig} from './VueConfig';

new Vue(
    Object.assign(VueConfig, {
        el: '#app',
        data: {
            msg: 'Oh my app',
        }
    })
);

```

效果如下:http://take.ms/wiPGR

对于 Vue2,可以这样尝试:

Vue.options.delimiters = ['${', '}'];