溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue技術(shù)棧開(kāi)發(fā)實(shí)戰(zhàn)--學(xué)習(xí)筆記1

發(fā)布時(shí)間:2020-07-04 12:41:49 來(lái)源:網(wǎng)絡(luò) 閱讀:4972 作者:295631788 欄目:web開(kāi)發(fā)

課程

vue技術(shù)棧開(kāi)發(fā)實(shí)戰(zhàn)

https://segmentfault.com/ls/1650000016221751

iview-admin作者 Lison 出品

個(gè)人學(xué)習(xí)筆記1-7

1 vue-cli 3.0

npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install -g @vue/cli

2 路由

path: '/argu/:name',
alias: '/home_page',
name: 'argu',
components: {
    default: () => import('@/views/child.vue'),
    email: () => import('@/views/email.vue'),  // 多個(gè)組件
    tel: () => import('@/views/tel.vue')
},
meta: {
    title: 'home'
},
children: []

redirect: to => '/'  //重定向

<router-view key="default"/>
<router-view key="email" name="email"/>
<router-view key="tel" name="tel"/>

{{ $route.params.name }}

this.$router.push({
    name: `argu`,
    params: {
        name: 'lison'
    },
    query:{}
    path:`/argu/${names}`
})

3 路由

{
  path: '/argu/:name',
  name: 'argu',
  component: () => import('@/views/argu.vue'),
  props: true

  props: {
    food: 'banana'  //第二種
  },
  props: route => ({  //第三種
    food: route.query.food
  }),
},

{{ food }}

export default {
  props: {
   food : {
      type: String,
      default: 'lison'
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      console.log(vm)
    })
  },
   beforeRouteLeave (to, from, next) {
       // const leave = confirm('您確定要離開(kāi)嗎?')
       // if (leave) next()
       // else next(false)
       next()
  },
  beforeRouteUpdate (to, from, next) {
    console.log(to.name, from.name)
  }
}

export const setTitle = (title) => {
    window.document.title = title || 'admin'
}

const HAS_LOGINED = true

router.beforeEach((to, from, next) => {
  to.meta && setTitle(to.meta.title)
  if (to.name !== 'login') {
    if (HAS_LOGINED) next()
    else next({ name: 'login' })
  } else {
    if (HAS_LOGINED) next({ name: 'home' })
    else next()
  }
})
1. 導(dǎo)航被觸發(fā)
2. 在失活的組件(即將離開(kāi)的頁(yè)面組件)里調(diào)用離開(kāi)守衛(wèi) beforeRouteLeave
3. 調(diào)用全局的前置守衛(wèi) beforeEach
4. 在重用的組件里調(diào)用 beforeRouteUpdate
5. 調(diào)用路由獨(dú)享的守衛(wèi) beforeEnter
6. 解析異步路由組件
7. 在被激活的組件(即將進(jìn)入的頁(yè)面組件)里調(diào)用 beforeRouteEnter
8. 調(diào)用全局的解析守衛(wèi) beforeResolve
9. 導(dǎo)航被確認(rèn)
10. 調(diào)用全局的后置守衛(wèi) afterEach
11. 觸發(fā)DOM更新
12. 用創(chuàng)建好的實(shí)例調(diào)用beforeRouterEnter守衛(wèi)里傳給next的回調(diào)函數(shù)
4 bus
父子組件    單向數(shù)據(jù)量                  父到子   屬性              子修改 事件修改

<input @input="handleInput" :value="value"/>  // 子組件

handleInput (event) {
  const value = event.target.value
  this.$emit('input', value)
}

<a-input @input="handleInput"/>   //父組件
<a-show :content="inputValue"/>

components: {
  AInput,
  AShow
},
methods: {
  handleInput (val) {   //獲取 
    this.inputValue = val
  }
}

兄弟組件

import Vue from 'vue'
const Bus = new Vue()
export default Bus

import Bus from './lib/bus'

Vue.config.productionTip = false
Vue.prototype.$bus = Bus

methods: {
  handleClick () {
    this.$bus.$emit('on-click', 'hello')
  }
}

mounted () {
  this.$bus.$on('on-click', mes => {
    this.message = mes
  })
}

5 vuex-- setter getter

const state = {
  appName: 'admin'
}
const getters = {
    appNameWithVersion: (state) => {
        return `${state.appName}v2.0`
}
}

const getters = {
    firstLetter: (state) => {
        return state.userName.substr(0, 1)
    }
}

export default state

import { mapState, mapGetters } from 'vuex'
computed: {
appName () {
  return this.$store.state.appName
},
...mapState({
  userName: state => state.user.userName
}),
...mapGetters([
  'appNameWithVersion',
  'firstLetter'
]),
}

6 vuex-mutation_action_module

const mutations = {            // 修改store值
  SET_APP_NAME (state, params) {
    state.appName = params
  },
  SET_APP_VERSION (state) {
vue.set(state, 'appVersion', 'v2.0')
  }
}
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'
methods: {
  ...mapMutations([
    'SET_USER_NAME',
    'SET_APP_NAME'
  ]),
  ...mapActions([
    'updateAppName'
  ]),
handleChangeAppName () {
  // this.$store.commit({
  //   type: 'SET_APP_NAME',
  //   appName: 'newAppName'
  // })
this.SET_APP_NAME({
    appName: 'newAppName'
})
}

const actions = {
  // updateAppName ({ commit }) {
  //   getAppName().then(res => {
  //     const { info: { appName } } = res
  //     commit('SET_APP_NAME', appName)
  //   }).catch(err => {
  //     console.log(err)
  //   })
  // }
  async updateAppName ({ commit }) {
    try {
      const { info: { appName } } = await getAppName()
      commit('SET_APP_NAME', appName)
    } catch (err) {
      console.log(err)
    }
  }
}

...mapActions([
  'updateAppName'
]),

7 vuex進(jìn)階   
持久化存儲(chǔ)

store

export default store => {
  if (localStorage.state) store.replaceState(JSON.parse(localStorage.state))
  store.subscribe((mutation, state) => {
    localStorage.state = JSON.stringify(state)
  })
}

import saveInLocal from './plugin/saveInLocal'
plugins: [ saveInLocal ]

strict 嚴(yán)格模式

<a-input v-model="stateValue"/>

SET_STATE_VALUE (state, value) {
  state.stateValue = value
}

stateValue: {
  get () {
    return this.$store.state.stateValue
  },
  set (val) {
    this.SET_STATE_VALUE(val)
  }
},
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI