我试图在一个*.vue
文件中定义一个const
:
<script>
export const CREATE_ACTION = 1,
export const UPDATE_ACTION = 2
<script>
然后在模板中使用它们:
<template>
...
<select :disabled="mode === UPDATE_ACTION">
....
</template>
但它不能工作。我如何在Vue模板中使用const
?
我试图在一个*.vue
文件中定义一个const
:
<script>
export const CREATE_ACTION = 1,
export const UPDATE_ACTION = 2
<script>
然后在模板中使用它们:
<template>
...
<select :disabled="mode === UPDATE_ACTION">
....
</template>
但它不能工作。我如何在Vue模板中使用const
?
如果您在数据中公开它们,就会使它们变得不必要地反应迅速,正如@mix3d所提到的那样...
更好的方法是将它们添加到Vue对象深入响应式原理中:
<template>
<div v-if="action === CREATE_ACTION">Something</div>
</template>
<script>
export default {
created() {
this.CREATE_ACTION = CREATE_ACTION;
this.UPDATE_ACTION = UPDATE_ACTION;
}
})
</script>
将它们暴露在您的数据上:
new Vue({
data:{
CREATE_ACTION: CREATE_ACTION,
UPDATE_ACTION: UPDATE_ACTION
}
})
如果您想在多个组件中使用此功能,可以使用插件:
// constsPlugin.js
const YOUR_CONSTS = {
CREATE_ACTION: 1,
UPDATE_ACTION: 2
...
}
let YourConsts = {}; // As suggested by the comments.
YourConsts.install = function (Vue, options) {
Vue.prototype.$getConst = (key) => {
return YOUR_CONSTS[key]
}
}
export default YourConsts
在main.js或者你定义new Vue()
的地方,你需要这样使用它:
import YourConsts from 'path/to/plugin'
Vue.use(YourConsts)
现在您可以像下面这样在组件模板中使用它:
<div>
<select :disabled="mode === $getConst('UPDATE_ACTION')">
</div>
YOUR_CONSTS
需要与YourConsts
大小写相同,因为我收到了错误消息“_YourConsts未定义_”。(ESLint也抱怨说YourConsts
未定义。) - Gino Mempin那么使用混合(Mixins)呢?这是我所使用的方式。不确定它是否是最佳或推荐的方式,但代码看起来更加清晰整洁。
data/actions.js
export const CREATE_ACTION = 1;
export const UPDATE_ACTION = 2;
export const actionsMixin = {
data() {
return {
CREATE_ACTION,
UPDATE_ACTION
}
}
}
MyComponent.vue
<template>
<div v-if="action === CREATE_ACTION">Something</div>
</template>
<script>
import {actionsMixin, CREATE_ACTION} from './data/actions.js';
export default {
mixins: [actionsMixin]
data() {
return {
action: CREATE_ACTION
}
}
}
</script>
created()
阶段声明它。 - Kalnode<template>
<div>
hello {{ fooConst }}
</div>
</template>
<script>
const fooConst = "bar";
export default {
setup() {
return {
fooConst,
}
},
}
</script>
或者完全使用组合 API:
<script setup>
const fooConst = "bar";
</script>
<template>
<div>
hello {{ fooConst }}
</div>
</template>
<template>
<div v-if="FOO_CONST.bar">Something</div>
</template>
<script>
import {FOO_CONST} from './const.js';
export default {
data() {
return {
FOO_CONST: Object.freeze(FOO_CONST) // this makes vue not reactive this data property
}
}
}
</script>
<template>
<div>
<p :style="{ color: $options.COLOR }">
{{$options.HELLO}} {{$options.PERSON}}
</p>
</div>
</template>
<script>
const HELLO = 'Hello there.';
const COLOR = 'red';
export default {
mounted() {
console.log('Hello world');
},
COLOR,
HELLO,
PERSON: 'General Kenobi!',
}
</script>
// Action.js
const Action = {
CREATE: 1,
UPDATE: 2
}
Action.Mixin = {
created () {
this.Action = Action
}
}
export default Action
然后在组件中:
<template>
<select :disabled="mode === Action.UPDATE">
</template>
<script>
import Action from './Action.js'
export default {
mixins: [Action.Mixin]
}
</script>
这就像是Alessandro Benoit和L. Palaiokostas答案的结合。
created
钩子动态生成:function mixinFactory(constants) { return { created() { Object.keys(constants).forEach((cName) => this[cName] = constants[cName]); }, }; }
- Tibi Neaguthis.constants = Object.freeze({ ONE: 1, TWO: 2 })
。 为了使其起作用,您需要将常量添加到自己的 frozen 对象中,并将其添加到this
的任意键中,而不是直接添加到this
中。 - Impirator