Vue-mock data in cli project
- Install http-server. Run with the project.
- The SRC path creates the mock folder to configure the DESIRED JSON data.
- The root path creates vue.config.js to set the proxy situation.
- In the mock. Start the HTTP server.
# yarn global add http-server
# touch vue.config.js
# mkdir src/mock
# touch src/mock/list.json
# cd mock
# http-server .
module.exports ={
devServer:{
proxy:{
'/list':{
target:'http://localhost:8081',
pathRewrite:{
'/list': 'list.json'
}
}
}
}
}
Copy the code
Vue-router Configures and uses routes
- Install the vue – youter
- SRC Root path to create router.js
- To introduce the build, use the vueRouter
- The main js into the router
router.js
# yarn add vue-router
# touch src/router.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import pageA from 'a.vue'
import pageB from 'b.vue'
Vue.user(VueRouter);
const routers=[{
path:'/',
component: pageA
},
{
path:'/b',
component: pageB
}]
let router=new VueRouter({routers})
export default router;
Copy the code
main.js
import Vue from 'vue'
import App from './App.vue'
import './directive/n'
import router from './router'
Vue.config.productionTip = false
new Vue({
router,
render: h => h(App),
}).$mount('#app')
Copy the code
Configure and use vuEX
Since we often encounter inter-component communication in the daily development process, of course. In vue.js parent-child communication, usually the parent component passes the value through props, and the sub-component is triggered by the user-defined event ¥emit, etc., including parent-grandson communication uses provide/Inject. However, multi-service modules and data between different modules are not coherent, so it is convenient to split vuex into multiple modules for code maintenance. And it also makes the functional modules high cohesion and low coupling
-
Vuex base use case
- Install vuex.
- Create store.js in the SRC path
- Customize state data, add mutations operation function, write trigger actions function
- The main js introduced vuex
- The last name introduced must be Store
- MapActions page to bring in defined methods;
store.js
# yarn add vuex
# touch src/store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state={
count:1
}
const mutations={
increment(state) {
state.count++
},
decrement(state) {
state.count--
}
}
const actions={
increment: ({
commit
}) => {
commit('increment')
},
decrement: ({
commit
}) => {
commit('decrement')}}export default new Vuex.Store({
state,
mutations,
actions
})
Copy the code
main.js
import Vue from 'vue'
import App from './App.vue'
import './directive/n'
import router from './router'
import store from './store/index'
Vue.config.productionTip = false
new Vue({
router,
store,
render: h => h(App),
}).$mount('#app')
Copy the code
a.vue
<template>
<div class="page">
<div> vuex {{$store.state.count}}</div>
<button @click="increment"</button> < button@click ="decrement"</button> </div> </template> <script> import {mapActions} from'vuex'
exportdefault { methods:{ ... mapActions(['increment'.'decrement'
])
}
}
</script>
Copy the code
-
Vuex multi-service module status management
- First, create index.js and modules folders under store file, which can write components of each module needed by our business, such as (A.js, B.js).
- Customize state data, add mutations operation function, write trigger actions function, and export the content we defined. Remember to add namespaced:true;
- Import the (a,b)js file we created in index.js. Here we use Modules to export the component content we create
- Page references bring in mapActions. Import methods through methods.
- Actions are triggered by user actions
- Action triggers, mutations changes the data. Finally, state updates the changed data and vUE content updates;
# mkdir src/store
# mkdir src/store/modules
# touch src/store/modules/a.js
# touch src/store/modules/b.js
# touch src/store/index.js
Copy the code
a.js
const state = {
count: 1
}
const mutations = {
add(state) {
state.count++
},
reduce(state) {
state.count--
}
}
const actions = {
add: ({
commit
}) => {
commit('add')
},
reduce: ({
commit
}) => {
commit('reduce')}}export default {
namespaced: true,
state,
mutations,
actions
}
Copy the code
b.js
]const state = {
money: 1
}
const mutations = {
add(state) {
state.money++
},
reduce(state) {
state.money--
}
}
const actions = {
add: ({
commit
}) => {
commit('add')
},
reduce: ({
commit
}) => {
commit('reduce')}}export default {
namespaced: true,
state,
mutations,
actions
}
Copy the code
index.js
import Vue from 'vue'
import Vuex from 'vuex'
import money from './modules/b'
import count from './modules/a'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
money,
count
}
})
Copy the code
Used in vUE pages
<template>
<div class="page">
<div> vuex {{$store.state.count.count}}</div>
<button @click="add"</button> < button@click ="reduce"</button> </div> </template> <script> import {mapActions} from'vuex'
exportdefault { methods: { ... mapActions('count'['add'.'reduce'
])
}
}
</script>
Copy the code
-
Vuex in mutations transfer parameters
- Just pass the parameter in the event function
- Mutations will do the operation
Vue page
<template>
<div class="page">
<div> vuex {{$store.state.count.count}}</div>
<button @click="add(3)"</button> < button@click ="reduce"</button> </div> </template> <script> import {mapActions} from'vuex'
exportdefault { methods: { ... mapActions('count'['add'.'reduce'
])
}
}
</script>
Copy the code
a.js
const state = {
count: 10
}
const mutations = {
add(state, param) {
state.count += param
},
reduce(state) {
state.count--
}
}
const actions = {
add: ({
commit
}, param) => {
commit('add', param)
},
reduce: ({
commit
}) => {
commit('reduce')}}export default {
namespaced: true,
state,
mutations,
actions
}
Copy the code