创建Vue时遇到的坑

# 全局安装 vue-cli$npm install --global vue-cli

# 创建一个基于 webpack 模板的新项目

$vue init webpack my-project

# 安装依赖,走你

$cd my-project

$npm install(如果失败添加--force)

$npm run dev

继续命令

npm install iview --save

添加vuex

npm install --save vuex
 
 
import Vue from 'vue'
import App from './App'
import router from './router'
import iView from 'iview' // 使用 iView
import 'iview/dist/styles/iview.css' // 使用 iView
import '../src/css/public.css'
import store from '../src/store/store.js'//Vuex
Vue. config. productionTip = false
Vue. use( iView)
Vue. use( store)
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
template: '<App/>',
components: { App }
})
新建文件(非必要,但是推荐)stores.js

import Vue from 'vue';
import Vuex from 'vuex';
Vue. use( Vuex);

export default new Vuex. Store({
state:{
count: 1000
}
,
mutations:{
add( state){
state. count+= 1;
},
reduce( state){
state. count-= 1;
}
,
mutationName( state) {
//在这里改变state中的数据
alert( '+-+-+-++');
}
}
});


其他页面调用


<template>
<div>
<h2>{{ msg}} </h2>
<hr/ >
<div>
<button @click=" handleClick()" > + </button>
<button @click=" $store. commit( 'reduce')" >- </button>
</div>
<h3>{{ $store. state. count}} </h3>
</div>
</template>
<script >
import store from "../store/store";
export default {
data() {
return {
msg: "Hello Vuex"
};
},
computed: {
count() {
return this. $store. state. count;
}
},
methods: {
handleClick() {
this. $store. commit( "mutationName");
}
}
};
</script>





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值