当前位置: 首页 > wzjs >正文

北京品牌建设网站公司排名wordpress newsroom

北京品牌建设网站公司排名,wordpress newsroom,淄博网站制作开发优化,阿里巴巴网站优化一、基于VueCli自定义创建项目 233 344 二、Vuex 初始准备 建项目的时候把vuex勾选上就不用再yarn add vuex3了 store/index.js // 这里面存放的就是vuex相关的核心代码 import Vuex from vuex import Vue from vue// 插件安装 Vue.use(Vuex)// 创建仓库(空仓库…

一、基于VueCli自定义创建项目

233

344

二、Vuex 初始准备

建项目的时候把vuex勾选上就不用再yarn add vuex@3了

store/index.js

// 这里面存放的就是vuex相关的核心代码
import Vuex from 'vuex'
import Vue from 'vue'// 插件安装
Vue.use(Vuex)// 创建仓库(空仓库)
const store = new Vuex.Store()// 到处给main.js使用
export default store

App.vue

  created () {// console.log(this.$router) // 没配console.log(this.$store) // 没配},

main.js

import Vue from 'vue'
import App from './App.vue'
import store from '@/store/index'
console.log(store.state.count)Vue.config.productionTip = falsenew Vue({render: h => h(App),store
}).$mount('#app')

三、通过vuex仓库访问数据

(1)通过store直接访问

 新建仓库数据,这样别的模块、组件都可以访问到仓库中的数据。

// 创建仓库(空仓库)
const store = new Vuex.Store({// 通过state可以提供数据,所有组件共享的数据state: {title: '大标题',count: 100}
}
)

例如,App.vue中的模块中(指的是template中)

    <h1>根组件--{{ $store.state.title }}--{{ $store.state.count }}</h1>

App.vue中的组件中(指的是script中) 

  created () {console.log(this.$store.state.count) // 没配},

Son.vue(模块template中) 

    从vuex中获取的值: <label>{{ $store.state.count }}</label>

main.js(js中)

console.log(store.state.count)

总结:只需要在App.vue中写好仓库,后面可以直接通过底层的逻辑去调用,即:

模板中:{{$store.state.xxx }}
组件逻辑中:this.$store.state.xxx
JS模块中:store.state.xxx

Son1.vue

<template><div class="box"><h2>Son1 子组件 {{ $store.state.title }}</h2>从vuex中获取的值: <label>{{ $store.state.count }}</label><br><button>值 + 1</button></div>
</template><script>
export default {name: 'Son1Com'
}
</script><style lang="css" scoped>
.box{border: 3px solid #ccc;width: 400px;padding: 10px;margin: 20px;
}
h2 {margin-top: 10px;
}
</style>

(2)通过辅助函数(简化)

mapState是辅助函数,帮助我们把store中的数据自动映射到组件的计算属性中

 App.vue

//展示一下 不用

import { mapState } from 'vuex'
console.log(mapState(['count', 'title']))

在App.vue中这么写,就可以直接调用使用里面的值

<template><div id="app"><h1>根组件--{{ title }}--{{ count }}</h1><input type="text"><Son1></Son1><hr><Son2></Son2></div>
</template>computed: {...mapState(['count', 'title'])},

Son2.vue,虽然写起来简单还是需要再写一遍解构函数,即  computed: {
    ...mapState(['count', 'title'])
  }

<template><div class="box"><h2>Son2 子组件 {{ title }}</h2>从vuex中获取的值:<label>{{ count }}</label><br /><button>值 - 1</button></div>
</template><script>
import { mapState } from 'vuex'
export default {name: 'Son2Com',computed: {...mapState(['count', 'title'])}
}
</script><style lang="css" scoped>
.box {border: 3px solid #ccc;width: 400px;padding: 10px;margin: 20px;
}
h2 {margin-top: 10px;
}
</style>

四、组件修改仓库中的数据(mutations)

(1)错误情况

<template><div class="box"><h2>Son1 子组件 {{ $store.state.title }}</h2>从vuex中获取的值: <label>{{ $store.state.count }}</label><br><button @click="handleAdd">值 + 1</button></div>
</template><script>
export default {name: 'Son1Com',methods: {handleAdd () {// 错误代码,不会报错,并且可以实现,但其实是错的 (但是vue不会监测 监测需要成本)// this.$store.state.count++// console.log(this.$store.state.count)// 应该通过mutation 核心概念 进行修改}}
}
</script><style lang="css" scoped>
.box{border: 3px solid #ccc;width: 400px;padding: 10px;margin: 20px;
}
h2 {margin-top: 10px;
}
</style>

(2)严格模式 strict:true

为了避免这种情况,方便初学者检查,我们采用严格模式。store/index.js中操作

// 这里面存放的就是vuex相关的核心代码
import Vuex from 'vuex'
import Vue from 'vue'// 插件安装
Vue.use(Vuex)// 创建仓库(空仓库)
const store = new Vuex.Store({// 严格模式(有利于初学者,检测不规范的代码 => 上线时需要关闭)// 其实还是可以修改,只是会提示报错strict: true,// 通过state可以提供数据,所有组件共享的数据state: {title: '大标题',count: 100}
}
)// 到处给main.js使用
export default store

(3)修改数据-mutations

 store/index.js

// 这里面存放的就是vuex相关的核心代码
import Vuex from 'vuex'
import Vue from 'vue'// 插件安装
Vue.use(Vuex)// 创建仓库(空仓库)
const store = new Vuex.Store({// 严格模式(有利于初学者,检测不规范的代码 => 上线时需要关闭)// 其实还是可以修改,只是会提示报错strict: true,// 1.通过state可以提供数据,所有组件共享的数据state: {title: '大标题',count: 100},// 2.通过mutations可以提供修改数据的方法,mutation是一个对象mutations: {//   所有的mutation函数,第一个参数,都是stateaddCount (state) {//   修改数据state.count += 1},addFive (state) {//   修改数据state.count += 5},changeTitle (state) {state.title = '小标题'}}}
)// 到处给main.js使用
export default store

Son1.vue

<template><div class="box"><h2>Son1 子组件 {{ $store.state.title }}</h2>从vuex中获取的值: <label>{{ $store.state.count }}</label><br><button @click="handleAdd">值 + 1</button><button @click="addFive">值 + 5</button><button @click="changeFn">改标题</button></div>
</template><script>
export default {name: 'Son1Com',methods: {handleAdd () {// 错误代码,不会报错,并且可以实现,但其实是错的 (但是vue不会监测 监测需要成本)// this.$store.state.count++// console.log(this.$store.state.count)// 应该通过mutation 核心概念 进行修改数据// 需要提交调用mutationthis.$store.commit('addCount')},addFive () {this.$store.commit('addFive')},changeFn () {this.$store.commit('changeTitle')}}
}
</script><style lang="css" scoped>
.box{border: 3px solid #ccc;width: 400px;padding: 10px;margin: 20px;
}
h2 {margin-top: 10px;
}
</style>

(4)mutations传参

index.js

  // 2.通过mutations可以提供修改数据的方法,mutation是一个对象mutations: {//   所有的mutation函数,第一个参数,都是state// addCount (state) {//   //   修改数据//   state.count += 1// },addCount (state, n) {//   修改数据state.count += n},changeTitle (state, newTitle) {state.title = newTitle}}

Son.vue

<template><div class="box"><h2>Son1 子组件 {{ $store.state.title }}</h2>从vuex中获取的值: <label>{{ $store.state.count }}</label><br><button @click="handleAdd(1)">值 + 1</button><button @click="handleAdd(5)">值 + 5</button><button @click="handleAdd(10)">值 + 10</button><button @click="changeFn">改标题</button></div>
</template><script>
export default {name: 'Son1Com',methods: {handleAdd (n) {// 错误代码,不会报错,并且可以实现,但其实是错的 (但是vue不会监测 监测需要成本)// this.$store.state.count++// console.log(this.$store.state.count)// 应该通过mutation 核心概念 进行修改数据// 需要提交调用mutation// this.$store.commit('addCount')this.$store.commit('addCount', n)console.log(n)},changeFn (newTitle) {this.$store.commit('changeTitle', '黑马程序员')}}
}
</script><style lang="css" scoped>
.box{border: 3px solid #ccc;width: 400px;padding: 10px;margin: 20px;
}
h2 {margin-top: 10px;
}
</style>

(5)mutations传多参

只能传递有且仅有一个参数,如果是想传递多个参数,可以写成对象数组的形式。

Son1.vue

<template><div class="box"><h2>Son1 子组件 {{ $store.state.title }}</h2>从vuex中获取的值: <label>{{ $store.state.count }}</label><br><button @click="handleAdd(1)">值 + 1</button><button @click="handleAdd(5)">值 + 5</button><button @click="handleAdd(10)">值 + 10</button><button @click="changeFn">改标题</button></div>
</template><script>
export default {name: 'Son1Com',methods: {handleAdd (n) {// 错误代码,不会报错,并且可以实现,但其实是错的 (但是vue不会监测 监测需要成本)// this.$store.state.count++// console.log(this.$store.state.count)// 应该通过mutation 核心概念 进行修改数据// 需要提交调用mutation// this.$store.commit('addCount')// this.$store.commit('addCount', n)// 只能传递有且仅有一个参数,如果是想传递多个参数,可以写成对象数组的形式。this.$store.commit('addCount', {count: n,msg: '哈哈'})console.log(n)},changeFn (newTitle) {this.$store.commit('changeTitle', '黑马程序员')}}
}
</script><style lang="css" scoped>
.box{border: 3px solid #ccc;width: 400px;padding: 10px;margin: 20px;
}
h2 {margin-top: 10px;
}
</style>

index.js

  // 2.通过mutations可以提供修改数据的方法,mutation是一个对象mutations: {addCount (state, obj) {//   修改数据state.count += obj.count}
}
)

 (6)实时输入,实时更新

不能用:value 因为不能直接改变仓库的值,但是:value是由:value和@input组成,所以可以分开用

App.vue

<template><div id="app"><h1>根组件--{{ title }}--{{ count }}</h1><input type="text" :value="count" @input="handleInput"><Son1></Son1><hr><Son2></Son2></div>
</template><script>
import { mapState } from 'vuex'
import Son1 from './components/Son1.vue'
import Son2 from './components/Son2.vue'// console.log(mapState(['count', 'title']))export default {name: 'app',data: function () {return {}},created () {// console.log(this.$router) // 没配// console.log(this.$store) // 没配// console.log(this.$store.state.count) // 没配},computed: {...mapState(['count', 'title'])},components: {Son1,Son2},methods: {handleInput (e) {// 1.实时获取输入框的值// e.target指得是输入框  +是转化成数字类型const num = +e.target.value// 2.提交mutation,调用mutation函数this.$store.commit('changeCount', num)}}
}
</script><style>
#app {width: 600px;margin: 20px auto;border: 3px solid #ccc;border-radius: 3px;padding: 10px;
}
</style>

index.js

    changeCount (state, newCount) {state.count = newCount}

五、mapMutations(辅助函数)

 Son2.vue

<template><div class="box"><h2>Son2 子组件 {{ title }}</h2>从vuex中获取的值:<label>{{ count }}</label><br /><button @click="handleSub(1)">值 - 1</button><button @click="handleSub(5)">值 - 5</button><button @click="handleSub(10)">值 - 10</button>
<!-- 上面可以直接用subCount()了,都没必要封装了 --><button @click="subCount(10)">值 - 10</button><button @click="changeTitle('改了标题')">改标题</button></div>
</template><script>
import { mapMutations, mapState } from 'vuex'
export default {name: 'Son2Com',computed: {...mapState(['count', 'title'])},methods: {...mapMutations(['subCount', 'changeTitle']),handleSub (n) {// this.$store.commit('subCount', n)this.subCount(n)}}
}
</script><style lang="css" scoped>
.box {border: 3px solid #ccc;width: 400px;padding: 10px;margin: 20px;
}
h2 {margin-top: 10px;
}
</style>

六、actions和getters

(1)actions和dispatch

目标:明确actions的基本用法,处理异步操作。

需求:一秒钟之后,修改state的count成666。

说明:mutations必须是同步的(便于检测数据变化,记录调试)

 index.js

// 这里面存放的就是vuex相关的核心代码
import Vuex from 'vuex'
import Vue from 'vue'// 插件安装
Vue.use(Vuex)// 创建仓库(空仓库)
const store = new Vuex.Store({// 严格模式(有利于初学者,检测不规范的代码 => 上线时需要关闭)// 其实还是可以修改,只是会提示报错strict: true,// 1.通过state可以提供数据,所有组件共享的数据state: {title: '大标题',count: 100},// 3.actions处理异步//   注意:不能直接操作state,操作state,还是需要commit mutationactions: {// context上下文(此处未分模块,可以当作store仓库)// context.commit('mutation名字',额外参数)changeCountAction (context, num) {//   这里是setTimeout模拟异步,以后大部分场景是发请求setTimeout(() => {context.commit('changeCount', num)}, 1000)}}
})// 到处给main.js使用
export default store

Son1.vue

<template><div class="box"><h2>Son1 子组件 {{ $store.state.title }}</h2>从vuex中获取的值: <label>{{ $store.state.count }}</label><br><button @click="handleAdd(1)">值 + 1</button><button @click="handleAdd(5)">值 + 5</button><button @click="handleAdd(10)">值 + 10</button><button @click="handleChange(66)">疫苗后改为66</button><button @click="changeFn">改标题</button></div>
</template><script>
export default {name: 'Son1Com',methods: {handleAdd (n) {// 错误代码,不会报错,并且可以实现,但其实是错的 (但是vue不会监测 监测需要成本)// this.$store.state.count++// console.log(this.$store.state.count)// 应该通过mutation 核心概念 进行修改数据// 需要提交调用mutation// this.$store.commit('addCount')// this.$store.commit('addCount', n)// 只能传递有且仅有一个参数,如果是想传递多个参数,可以写成对象数组的形式。this.$store.commit('addCount', {count: n,msg: '哈哈'})// console.log(n)},changeFn (newTitle) {this.$store.commit('changeTitle', '黑马程序员')},handleSub (n) {this.$store.commit('subCount', n)},handleChange () {// 调用action// this.$store.dispatch('action名字',额外参数)this.$store.dispatch('changeCountAction', '666')}}
}
</script><style lang="css" scoped>
.box{border: 3px solid #ccc;width: 400px;padding: 10px;margin: 20px;
}
h2 {margin-top: 10px;
}
</style>

(2)辅助函数-mapActions

目标:掌握辅助函数mapActions,映射方法,这样可以简化dispatch

Son2.vue

<template><div class="box"><h2>Son2 子组件 {{ title }}</h2>从vuex中获取的值:<label>{{ count }}</label><br /><button @click="handleSub(1)">值 - 1</button><button @click="handleSub(5)">值 - 5</button><button @click="subCount(10)">值 - 10</button>
<!-- 上面可以直接用subCount()了,都没必要封装了 --><button @click="subCount(10)">值 - 10</button><button @click="changeTitle('改了标题')">改标题</button><button @click="changeCountAction(888)">一秒后改成888</button></div>
</template><script>
import { mapActions, mapMutations, mapState } from 'vuex'
export default {name: 'Son2Com',computed: {...mapState(['count', 'title'])},methods: {...mapMutations(['subCount', 'changeTitle']),handleSub (n) {// this.$store.commit('subCount', n)this.subCount(n)},...mapActions(['changeCountAction'])}
}
</script><style lang="css" scoped>
.box {border: 3px solid #ccc;width: 400px;padding: 10px;margin: 20px;
}
h2 {margin-top: 10px;
}
</style>

(3)getters

也就是对仓库中的数据(state)进行操作

index.js

// 这里面存放的就是vuex相关的核心代码
import Vuex from 'vuex'
import Vue from 'vue'// 插件安装
Vue.use(Vuex)// 创建仓库(空仓库)
const store = new Vuex.Store({// 严格模式(有利于初学者,检测不规范的代码 => 上线时需要关闭)// 其实还是可以修改,只是会提示报错strict: true,// 1.通过state可以提供数据,所有组件共享的数据state: {title: '大标题',count: 100,list: [1, 2, 3, 4, 5, 6, 7, 8, 9]},// 2.通过mutations可以提供修改数据的方法,mutation是一个对象mutations: {//   所有的mutation函数,第一个参数,都是state// addCount (state) {//   //   修改数据//   state.count += 1// },// addCount (state, n) {//   //   修改数据//   state.count += n// },addCount (state, obj) {//   修改数据state.count += obj.count},changeTitle (state, newTitle) {state.title = newTitle},subCount (state, n) {state.count -= n},changeCount (state, newCount) {state.count = newCount}},// 3.actions处理异步//   注意:不能直接操作state,操作state,还是需要commit mutationactions: {// context上下文(此处未分模块,可以当作store仓库)// context.commit('mutation名字',额外参数)changeCountAction (context, num) {//   这里是setTimeout模拟异步,以后大部分场景是发请求setTimeout(() => {context.commit('changeCount', num)}, 1000)}},// 4.getters类似于计算属性// 但是只有获取,没有修改,修改只能通过mutationsgetters: {// 注意点:// 1.形参第一个参数,就是state// 必须有返回值,返回值就是getters的值filterList (state) {return state.list.filter(item => item > 5)}}
})// 到处给main.js使用
export default store

Son1.vue(纯原生)

<template><div class="box"><h2>Son1 子组件 {{ $store.state.title }}</h2>从vuex中获取的值: <label>{{ $store.state.count }}</label><br><button @click="handleAdd(1)">值 + 1</button><button @click="handleAdd(5)">值 + 5</button><button @click="handleAdd(10)">值 + 10</button><button @click="handleChange">一秒后改为666</button><button @click="changeFn">改标题</button><hr><div>{{ $store.state.list }}</div><div>{{ $store.getters.filterList }}</div></div>
</template><script>
export default {name: 'Son1Com',methods: {handleAdd (n) {// 错误代码,不会报错,并且可以实现,但其实是错的 (但是vue不会监测 监测需要成本)// this.$store.state.count++// console.log(this.$store.state.count)// 应该通过mutation 核心概念 进行修改数据// 需要提交调用mutation// this.$store.commit('addCount')// this.$store.commit('addCount', n)// 只能传递有且仅有一个参数,如果是想传递多个参数,可以写成对象数组的形式。this.$store.commit('addCount', {count: n,msg: '哈哈'})// console.log(n)},changeFn (newTitle) {this.$store.commit('changeTitle', '黑马程序员')},handleChange () {// 调用action// this.$store.dispatch('action名字',额外参数)this.$store.dispatch('changeCountAction', 666)}}
}
</script><style lang="css" scoped>
.box{border: 3px solid #ccc;width: 400px;padding: 10px;margin: 20px;
}
h2 {margin-top: 10px;
}
</style>

(4)辅助函数-mapGetters 

Son2.vue(辅助函数)

<template><div class="box"><h2>Son2 子组件 {{ title }}</h2>从vuex中获取的值:<label>{{ count }}</label><br /><button @click="handleSub(1)">值 - 1</button><button @click="handleSub(5)">值 - 5</button><button @click="subCount(10)">值 - 10</button>
<!-- 上面可以直接用subCount()了,都没必要封装了 --><button @click="subCount(10)">值 - 10</button><button @click="changeTitle('改了标题')">改标题</button><button @click="changeCountAction(888)">一秒后改成888</button>
<hr>
<div>{{ filterList }}</div></div>
</template><script>
import { mapActions, mapGetters, mapMutations, mapState } from 'vuex'
export default {name: 'Son2Com',computed: {// mapState和mapGetters都是映射属性...mapState(['count', 'title']),...mapGetters(['filterList'])},methods: {handleSub (n) {// this.$store.commit('subCount', n)this.subCount(n)},// mapMutations和mapActions都是映射方法...mapMutations(['subCount', 'changeTitle']),...mapActions(['changeCountAction'])}
}
</script><style lang="css" scoped>
.box {border: 3px solid #ccc;width: 400px;padding: 10px;margin: 20px;
}
h2 {margin-top: 10px;
}
</style>

七、模块创建


文章转载自:

http://gGJ4S5B9.rmdsd.cn
http://BeTJNiCU.rmdsd.cn
http://eqVcJQFT.rmdsd.cn
http://rmR7QiU7.rmdsd.cn
http://X65s16Su.rmdsd.cn
http://WU2VZUn5.rmdsd.cn
http://9I3eyFGu.rmdsd.cn
http://6fvRjrvC.rmdsd.cn
http://TzKG3YBQ.rmdsd.cn
http://pGYaLZ1S.rmdsd.cn
http://rE6c9lYh.rmdsd.cn
http://VJxbjYR3.rmdsd.cn
http://5qLsbLNE.rmdsd.cn
http://kOek0uvT.rmdsd.cn
http://8dh9qgfJ.rmdsd.cn
http://POc9r6tK.rmdsd.cn
http://hpr21myD.rmdsd.cn
http://f51n3UkC.rmdsd.cn
http://hWLv4orn.rmdsd.cn
http://At2uOidM.rmdsd.cn
http://1xjo0GR1.rmdsd.cn
http://MNx1Otl8.rmdsd.cn
http://6mffDeB3.rmdsd.cn
http://6CSUNkYI.rmdsd.cn
http://IyidsNCs.rmdsd.cn
http://20Tc2vLW.rmdsd.cn
http://ob2haoIs.rmdsd.cn
http://608is4KX.rmdsd.cn
http://Z8ivsGeO.rmdsd.cn
http://csg2L5su.rmdsd.cn
http://www.dtcms.com/wzjs/705804.html

相关文章:

  • 做微商有什么好的货源网站济宁竞价托管
  • 家具网站策划书设计logo网站赚钱
  • 南京做代账会计在哪个网站上找家政公司响应式网站建设案例
  • 网站开发前期准备门户网站设计说明
  • 万江做网站北京网络营销培训
  • 行业网站网址医疗网站建设渠道
  • wordpress入门建站教程二建筑方案设计流程步骤
  • 个人备案网站做电影站查网站是什么公司做的
  • 做网站下载那个数据库好电子商务主要学什么内容
  • 做网站设计都需要什么数码类网站名称
  • 深圳动态科技集团网站互联网建站网站
  • 广州市网站建设 骏域贵阳网络营销推广专家
  • 网站怎么没有排名做网站一定需要虚拟主机吗
  • 网站模板 自适应京东店铺购买平台
  • 成都营销型网站建设及推广那家好四川seo推广
  • 怎么随便搞个网站网址关键词查询
  • 网站应该怎么做的网页设计培训班
  • 长沙商业网站建设淄博论坛网站建设
  • 上海网站定制公司怎么免费弄网站
  • 手机网站和电脑网站样式的区别厦门找一家做网站的公司
  • 问卷调查网站怎么做自适应平台网站模板
  • 郑州高端网站建设团队阿里云服务器租用
  • 青岛网站域名备案查询镇海官方网站建设
  • 个人做流量大的网站申请注册邮箱
  • 石家庄seo网站建设互联网内容服务商
  • 广西南宁电商网站建设用静态网站更新
  • wordpress自动网站地址域名的作用
  • 烟台网站推广排名关于网站建设文章
  • 网站建设 源码无锡百度推广代理商
  • 哪个网站可以做图交易平台广州建工集团有限公司官网