我可以通过我的vuejs应用程序中的商店创建一个常量,但我认为这不是一个好习惯.还有其他方法可以做同样的事情吗?
weg*_*rer 13
您始终可以在Vue应用程序范围之外定义变量,并在整个应用程序中使用它.
但是,如果您使用任何捆绑器,如Webpack/Browserify/etc. 你也可以这样做但你必须将它导入到使用它的每个组件中.一个例子可以在下面找到.
//const.js
export default {
c1: 'Constant 1',
c2: 'Constant 2'
}
// component.vue
import const from './const';
export default {
methods: {
method() {
return const.c1;
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以使用该方法
const State= Object.freeze({ Active: 1, Inactive: 2 });
export default {
data() {
return {
State,
state: State.Active
};
},
methods: {
method() {
return state==State.Active;
}
}
}
Run Code Online (Sandbox Code Playgroud)
或者
const State= Object.freeze({ Active: 1, Inactive: 2 });
export default {
data() {
return {
State_: State,
state: State.Active
};
},
methods: {
method() {
return state==State_.Active;
}
}
}
Run Code Online (Sandbox Code Playgroud)