Introduction of Vue Family Bucket

Introduction of Vue Family Bucket

 

Vue has a famous family bucket series, including vue-router ( http://router.vuejs.org ), vuex ( http://vuex.vuejs.org ), vue-resource ( https://github.com/ pagekit/vue-resource ). Coupled with the build tool vue-cli, sass style, is the core composition of a complete vue project.

To sum up: 1. Project construction tool, 2. Routing, 3. Status management, 4. HTTP request tool.

The following is a separate introduction

Foreword: Vue's two core ideas: componentization and data-driven. Componentization: split the whole into reusable individuals, data-driven: directly affect the BOM display through data changes, avoiding DOM operations.

1. Vue-cli is a scaffold for quickly building this single-page application,

# Install vue-cli globally
$ npm install --global vue-cli
# Create a new project based on the webpack template
$ vue init webpack my-project
# Install dependencies, go
$ cd my-project
$ npm install
$ npm run dev

2. vue-router

Install: npm installvue-router

If you use it in a modular project, you must  Vue.use() install the routing function explicitly by:

import  Vue from 'view'
import  VueRouter from 'vue-router'
Vue.use(VueRouter)

Also note that in use, you can use vue's transition properties to render the effect of switching pages.

3. vuex

The state management developed by vuex for vue.js applications can be understood as global data management. Vuex is mainly composed of five parts: state action, mutation, getters, and mudle.

The usage process is: The above four parts can be called directly in the component except mudle,

1、state

Similar to vue object data, used to store data and state. The stored data is responsive. If the data changes, the components that depend on the data will also change accordingly.

Examples of two ways to get state:

1.store.getters ['getRateUserInfo']
2. ...mapGetters({
        UserInfo: 'login/UserInfo', // user info
        menuList: 'getMenuList', // approve tariff approval
        RateUserInfo: 'getRateUserInfo' // Rate user information
   })
Note: 可以通过mapState把全局的state getters 映射到当前组件的 computed计算属性中。

2、actions

Action is triggered by the store.dispatch method: action supports asynchronous calls (api can be called), mutation only supports operation synchronization, and action submits mutation instead of directly changing state.

E.g:

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  },
  actions: {
    increment (context) {
      context.commit('increment')
    }
  }
})

The Action function accepts a context object with the same methods and properties as the store instance, so you can call context.commit to commit a mutation, or get state and getters via context.state and context.getters .

 

In practice, we often use ES2015's  parameter destructuring  to simplify code (especially when we need to call  commit it many times):

actions:{
  increment ({ commit }){
    commit('increment')
  }
}

 

3、mutation

Each mutation has a string event type (type) and a callback function (handler). This callback function is where we actually make the state change, and it accepts state as the first parameter.

4、getters

Vuex allows us to define "getters" (which can be thought of as computed properties of the store) in the store. Just like computed properties, the return value of a getter is cached based on its dependencies, and only recalculated when its dependencies change.

const getters = {
  getRateInitData: state => state.rateInitData,
  getchooseRateObj: state => state.chooseRateObj,
  getSearchRateParams: state => state.searchRateParams,
  getSearchRateResult: state => state.searchRateResult,
  getRateUserInfo: state => state.RateUserInfo,
  getMenuList: state => state.menuList,
  getRateQueryParams: state => state.rateQueryParams,
  getRateQueryResult: state => state.rateQueryResult,
  getCheckRateDetailParams: state => state.checkRateDetailParams,
  getReferenceCondition: state => state.referenceCondition,
  getWaitApprovalParams: state => state.waitApprovalParams
}

mapGetters helper function

The mapGetters helper function simply maps getters in the store to local computed properties:

 

Fourth, axios

axios is an http request package. Vue official website recommends using axios for http calls.

Install:

npm install axios --save

example:

1. Send a GETrequest

//通过给定的ID来发送请求
axios.get('/user?ID=12345')
  .then(function(response){
    console.log(response);
  })
  .catch(function(err){
    console.log(err);
  });
//以上请求也可以通过这种方式来发送
axios.get('/user',{
  params:{
    ID:12345
  }
})
.then(function(response){
  console.log(response);
})
.catch(function(err){
  console.log(err);
});
2发送一个POST请求
axios.post('/user',{
  firstName:'Fred',
  lastName:'Flintstone'
})
.then(function(res){
  console.log(res);
})
.catch(function(err){
  console.log(err);
});

Specific reference: https://www.jianshu.com/p/df464b26ae58

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325128121&siteId=291194637