在大多数 Vue.js 教程中,我看到这样的内容:
new Vue({
store, // inject store to all children
el: '#app',
render: h => h(App)
})
但我正在使用vue-cli(实际上是使用quasar),它为我声明了Vue实例,因此我不知道应该在哪里指定我想要store
成为“全局”的Vue变量。我在哪里可以指定?谢谢。
在大多数 Vue.js 教程中,我看到这样的内容:
new Vue({
store, // inject store to all children
el: '#app',
render: h => h(App)
})
但我正在使用vue-cli(实际上是使用quasar),它为我声明了Vue实例,因此我不知道应该在哪里指定我想要store
成为“全局”的Vue变量。我在哪里可以指定?谢谢。
是的,你可以在入口文件(main.js)中像这样设置这些变量:
Vue.store= Vue.prototype.store = 'THIS IS STORE VARIABLE';
然后您可以在Vue实例中像这样访问它:
<script>
export default {
name: 'HelloWorld',
methods: {
yourMethod() {
this.store // can be accessible here.
}
}
}
</script>
从评论区中关于Quasar模板中“没有入口文件”的讨论中得知。
你可以前往src/router/index.js,然后通过Vue获取全局变量进行设置:
...
import routes from './routes'
Vue.prototype.a = '123';
Vue.use(VueRouter)
...
然后,如果你在App.vue
中使用console.log输出它,就会得到类似以下的结果:
<script>
export default {
name: 'App',
mounted() {
console.log(this.a);
}
}
</script>
您也可以在App.vue
文件的脚本标签中执行相同的操作。
store
设置为全局变量,因为每个组件(this.$store
)和Vue实例在初始声明后都可以访问到该存储库。import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
updateCount(state) {
state.count += 1
}
}
})
main.js
import App from './App.vue'
import store from '/path/to/store.js'
new Vue({
el: '#app',
store,
render: h => h(App)
})
store
,则可以导入它(如我们在main.js
中所做的)并直接使用它【请注意,这是一种不好的实践方法】或使用this.$store
进行访问。您可以在此处了解更多信息。
return this.$store.state.showcase.drawerState
,但这不起作用,因为 this.$store
没有定义。我最初没有使用 Vuex 初始化项目,后来添加了它,也许这就是为什么它没有被自动处理的原因。不过其他答案都可以解决问题,我会接受其中一个答案。谢谢。 - Louis Amelinestore
作为实例属性,因为它已经存在 :) - GMaioloVue.prototype.$appName = 'My App'
new Vue({
beforeCreate: function() {
console.log(this.$appName)
}
})
稍晚加入节目,但我个人在Quasar
中使用的方法是创建一个Boot
文件来存储我的全局常量和变量。
我创建了这个Boot文件(我称之为global-constants.js
,但您可以自由地命名它)。
/src/boot/global-constants.js
import Vue from 'vue'
Vue.prototype.globalConstants = {
baseUrl: {
website: 'https://my.fancy.website.example.com',
api: 'https://my.fancy.website.example.com/API/v1'
}
}
if (process.env.DEV) {
Vue.prototype.globalConstants.baseUrl.website = 'http://localhost'
Vue.prototype.globalConstants.baseUrl.api = 'http://localhost/API/v1'
}
if (process.env.DEV) {
console.log('Global Constants:')
console.log(Vue.prototype.globalConstants)
}
然后在quasar.conf.js
文件中添加一行,以启动您的引导文件:
/quasar.conf.js
module.exports = function (ctx) {
return {
boot: [
'i18n',
'axios',
'notify-defaults',
'global-constants' // Global Constants and Variables
],
然后使用它:
this._vm.globalConstants.baseUrl.api
axios.post(this._vm.globalConstants.baseUrl.api + '/UpdateUserPreferences/', payload)
{{ globalConstants.baseUrl.api }}
this.globalConstants.baseUrl.api
index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default function (/* { ssrContext } */) {
const Store = new Vuex.Store({
modules: {
// example
},
state: {
cdn_url: 'https://assets.yourdomain.com/'
},
// for dev mode only
strict: process.env.DEV
})
return Store
}
...然后在您的组件中,例如YourPage.vuex
export default {
name: 'YourPage',
loadImages: function () {
img.src = this.$store.state.cdn_url + `yourimage.jpg`
}
}
一种Vue3的替代方法,关于this answer:
// Vue3
const app = Vue.createApp({})
app.config.globalProperties.$appName = 'My App'
app.component('child-component', {
mounted() {
console.log(this.$appName) // 'My App'
}
})