欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解vue之mixin的使用

 更新時(shí)間:2021年11月25日 17:19:53   作者:不求人0  
這篇文章主要為大家介紹了vue之mixin的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

vue之mixin的使用

  • 作用:在引入組件之后,則是將組件內(nèi)部的內(nèi)容如data等方法、method等屬性與父組件相應(yīng)內(nèi)容進(jìn)行合并。相當(dāng)于在引入后,父組件的各種屬性方法都被擴(kuò)充了
  • data數(shù)據(jù)的等訪問原則:若是使用mixin的當(dāng)前組件有該 data數(shù)據(jù) 或者 methods方法的話 直接運(yùn)用的是當(dāng)前組件的數(shù)據(jù)或者方法,否則的話直接繼承mixin內(nèi)部的數(shù)據(jù)與方法
  • 注意點(diǎn):可以定義共用的變量,在每個(gè)組件中使用,引入組件中之后,各個(gè)變量是相互獨(dú)立的,值的修改在組件中不會(huì)相互影響
  • 注意點(diǎn)2:mixin是在引入組件之后與組件中的對(duì)象和方法進(jìn)行合并,相當(dāng)于擴(kuò)展了父組件的data數(shù)據(jù)與方法等,可以理解為形成了一個(gè)新的組件

mixin之中的data數(shù)據(jù)訪問

mixin / index.js

export default {
  data () {
    return {
      msg: "我是mixin內(nèi)的msg"
    }
  },
  created () {
  },
  mounted () { },
  methods: {
  }
}

Home.vue

  • 在Home組件中使用mixin
<template>
  <div>
    <div>home -- {{ msg }}</div> /* home修改的msg */
  </div>
</template>
<script>
import mixin from "@/mixin/index.js";
export default {
  name: "Home",
  mixins: [mixin],
  data() {
    return {    };
  },
  created() {
    // 拿mixin的data數(shù)據(jù)
    console.log("home打印一下", this.msg); //home打印一下 我是mixin內(nèi)的msg
    // 修改mixin的data數(shù)據(jù)
    this.msg = "home修改的msg";
    console.log("home打印一下", this.msg); // home打印一下 home修改的msg
  },
  methods: {
  },
};
</script>
<style  lang="scss" scoped>
</style>

About2.vue

<template>
  <div>
    <div>about2 -- {{ msg }}</div> /*  about2修改的msg */
  </div>
</template>
<script>
import mixin from "@/mixin/index.js";
export default {
  name: "About2",
  mixins: [mixin],
  components: {},
  data() {
    return {
      msg: "本地的msg",
    };
  },
  created() {
    console.log("about2打印一下", this.msg); // 本地的msg
    this.msg = "about2修改的msg";
    console.log("about2打印一下", this.msg); // about2修改的msg
    // 最后頁(yè)面 顯示的 about2修改的msg 這個(gè)數(shù)據(jù)
  },
  methods: {
  },
};
</script>
<style  lang="scss" scoped>
</style>

mixin中的 methods方法和computed使用

mixin / index.js

export default {
  data () {
    return {
      msg: "我是mixin內(nèi)的msg"
    }
  },
  created () { },
  mounted () { },
  computed: {
    UserName () {
      return "我是計(jì)算屬性的UserName";
    },
  },
  methods: {
    tipMsg () {
      console.log("minxin內(nèi)的tipMsg方法", this.msg);
    },
    tipInfo (info) {
      console.log("minxin內(nèi)的tipInfo方法", info);
    }
  }
}

Home.vue

<template>
  <div>
    <div>home --- msg-{{ msg }} UserName-{{ UserName }}</div>
    /* home --- msg-home修改的msg UserName-我是計(jì)算屬性的UserName */
  </div>
</template>
<script>
import mixin from "@/mixin/index.js";
export default {
  name: "Home",
  mixins: [mixin],
  components: {},
  data() {
    return {};
  },
  created() {
    // 拿mixin的data數(shù)據(jù)
    console.log("home打印一下", this.msg); //home打印一下 我是mixin內(nèi)的msg
    // 修改mixin的data數(shù)據(jù)
    this.msg = "home修改的msg";
    console.log("home打印一下", this.msg); // home打印一下 home修改的msg
    // 調(diào)用mixin中的 tipMsg 方法
    this.tipMsg(); // minxin內(nèi)的tipMsg方法 home修改的msg
    this.tipInfo("我是home的菜雞info"); // minxin內(nèi)的tipInfo方法 我是home的菜雞info
  },
  methods: {},
};
</script>
<style  lang="scss" scoped>
</style>

About2.vue

<template>
  <div>
    <div>about2 -- {{ msg }} UserName-{{ UserName }}</div>
    /* about2 -- about2修改的msg UserName-我是計(jì)算屬性的UserName */
  </div>
</template>
<script>
import mixin from "@/mixin/index.js";
export default {
  name: "About2",
  mixins: [mixin],
  components: {},
  data() {
    return {
      msg: "本地的msg",
    };
  },
  created() {
    console.log("about2打印一下", this.msg); // 本地的msg
    this.msg = "about2修改的msg";
    console.log("about2打印一下", this.msg); // about2修改的msg
    // 最后頁(yè)面 顯示的 about2修改的msg 這個(gè)數(shù)據(jù)
    this.tipMsg(); // 最后打印 -> 我是about2本地的tipMsg方法
    this.tipInfo(); // minxin內(nèi)的tipInfo方法 undefined
  },
  methods: {
    tipMsg() {
      console.log("我是about2本地的tipMsg方法");
    },
  },
};
</script>

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • vue element 關(guān)閉當(dāng)前tab 跳轉(zhuǎn)到上一路由操作

    vue element 關(guān)閉當(dāng)前tab 跳轉(zhuǎn)到上一路由操作

    這篇文章主要介紹了vue element 關(guān)閉當(dāng)前tab 跳轉(zhuǎn)到上一路由操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 簡(jiǎn)單的vue-resourse獲取json并應(yīng)用到模板示例

    簡(jiǎn)單的vue-resourse獲取json并應(yīng)用到模板示例

    本篇文章主要介紹了簡(jiǎn)單的vue-resourse獲取json并應(yīng)用到模板示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2017-02-02
  • Vue手把手教你擼一個(gè) beforeEnter 鉤子函數(shù)

    Vue手把手教你擼一個(gè) beforeEnter 鉤子函數(shù)

    這篇文章主要介紹了Vue手把手教你擼一個(gè) beforeEnter 鉤子函數(shù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • vue 兄弟組件的信息傳遞的方法實(shí)例詳解

    vue 兄弟組件的信息傳遞的方法實(shí)例詳解

    這篇文章主要介紹了vue 兄弟組件的信息傳遞的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Vue中Object.defineProperty用法示例

    Vue中Object.defineProperty用法示例

    Vue中的Object.defineProperty是一個(gè)比較重要的方法,它是可以定義對(duì)象中屬性的一個(gè)方法,相比于在對(duì)象中直接定義的對(duì)象,它更具有靈活性,本文將通過代碼示例給大家簡(jiǎn)單介紹一下Vue中的Object.defineProperty,需要的朋友可以參考下
    2023-08-08
  • Vue中正確使用Element-UI組件的方法實(shí)例

    Vue中正確使用Element-UI組件的方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于Vue中正確使用Element-UI組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • iview實(shí)現(xiàn)select tree樹形下拉框的示例代碼

    iview實(shí)現(xiàn)select tree樹形下拉框的示例代碼

    這篇文章主要介紹了iview實(shí)現(xiàn)select tree樹形下拉框的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • vue.js 動(dòng)態(tài)組件詳解

    vue.js 動(dòng)態(tài)組件詳解

    這篇文章主要介紹了vue.js 動(dòng)態(tài)組件詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • vue實(shí)現(xiàn)滑動(dòng)超出指定距離回頂部功能

    vue實(shí)現(xiàn)滑動(dòng)超出指定距離回頂部功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)滑動(dòng)超出指定距離回頂部功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Element-ui中元素滾動(dòng)時(shí)el-option超出元素區(qū)域的問題

    Element-ui中元素滾動(dòng)時(shí)el-option超出元素區(qū)域的問題

    這篇文章主要介紹了Element-ui中元素滾動(dòng)時(shí)el-option超出元素區(qū)域的問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-05-05

最新評(píng)論