vue.js如何安装vuex
时间:2022-02-11 17:42
vue.js安装vuex的方法:首先进入项目路径后,在项目终端输入相关代码;然后在项目的package json文件中显示vuex插件;接着在项目src目录中新建store js;最后在【main.js】中应用vuex即可。 本教程操作环境:windows7系统、Vue2.9.6版,该方法适用于所有品牌电脑。 vue.js安装vuex的方法: 1、安装: 进入项目路径后,在项目终端输入: 2、安装完成后,会在项目的package.json文件中显示vuex插件,如下: 3、再项目src目录中新建store.js,文件内容如下: 4、在main.js中应用vuex: 导入store,然后再new Vue中使用,代码如下: 相关免费学习推荐:javascript(视频) 以上就是vue.js如何安装vuex的详细内容,更多请关注gxlsystem.com其它相关文章!npm install vuex --save
或者 cnpm install vuex --save
"dependencies": {
"vue": "^2.5.2",
"vue-router": "^3.0.1",
"vuex": "^3.3.0"
},
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
export default new Vuex.Store({
//所有的数据都放在state中
state:{},
//操作数据,唯一的通道是mutations
mutations:{},
//actions,可以来做异步操作,然后提交给mutations,而后再对state(数据)进行操作
actions:{}
})
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store' //导入store
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store, //在new Vue中使用
components: { App },
template: '<App/>'
})