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

Vue執(zhí)行方法,方法獲取data值,設(shè)置data值,方法傳值操作

 更新時間:2020年08月05日 09:15:06   作者:chesterdotchen  
這篇文章主要介紹了Vue執(zhí)行方法,方法獲取data值,設(shè)置data值,方法傳值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

方法寫在methods中

v-on:click="run()"

@click="run()"

方法獲取data中的數(shù)據(jù)通過this.數(shù)據(jù)獲取

方法設(shè)置data中的數(shù)據(jù)通過this.數(shù)據(jù)=''設(shè)置

例如通過this.list=[1,2,3],設(shè)置list的值,讓頁面出循環(huán)list

可通過 @click="run('123')",將值傳到方法中

可通過 @click="run($event)",將事件對象傳到方法中,然后根據(jù)事件對象的e.srcElement設(shè)置點擊標(biāo)簽的屬性,也可以通過e.srcElement.dataset獲取自定義屬性

<template>
 <div id="app">
  {{msg}}
  <button @click="run()">@click事件觸發(fā)</button>
  <button v-on:click="runvon()">v-on:click事件觸發(fā)</button>
  <button @click="getMsg()">獲取data.msg</button>
  <button v-on:click="setMsg()">設(shè)置data.msg</button>
  <ul>
   <li v-for="item in list">{{item}}</li>
  </ul>
  <button @click="setList()">設(shè)置List的值</button>
  <button @click="sendData('123')">方法傳值</button>
  <button v-on:click="sendEvent($event)">傳遞事件對象</button>
  <button @click="sendEventSet($event)">傳遞事件對象,并設(shè)置背景色</button>
  <button data-a="啦啦啦" @click="sendEventData($event)">傳遞事件對象,并獲取自定義屬性</button>
 </div>
</template>

<script>
export default {
 name: 'app',
 data () {
  return {
   msg: '123',
   list:[]
  }
 },
 methods:{
  run(){
   alert("@click事件觸發(fā)")
  },
  runvon(){
   alert("v-on:click事件觸發(fā)")
  },
  getMsg(){
   alert("我獲取到了msg"+this.msg);
  },
  setMsg(){
   this.msg="我是設(shè)置后的值"
  },
  setList(){
   for(var i = 0 ; i < 10 ; i++){
    this.list.push(i)
   }
   
  },
  sendData(a){
   alert("穿過來的值是:"+a)
  },
  sendEvent(e){
   console.log(e);
  },
  sendEventSet(e){
   e.srcElement.style.background="red";
  },
  sendEventData(e){
   alert(e.srcElement.dataset.a)
   
  }
 }
}
</script>
<style lang="scss">
</style>

補充知識:Vue 兄弟組件通過事件廣播傳遞數(shù)據(jù)

非父子組件傳值

通過事件廣播實現(xiàn)非父子組件傳值

1.新建js,引入并實例化Vue

import Vue from 'vue'
var VueEvent = new Vue();
export default VueEvent;

2.子組件A中引入VueEvent,并廣播事件

import VueEvent from '../model/VueEvent.js'

VueEvent.$emit('to-news',this.msg);

3.子組件B中引入VueEvent,并監(jiān)聽事件

import VueEvent from '../model/VueEvent.js'

VueEvent.$on('to-news',data=>{console.log(data);});

示例代碼

import Vue from 'vue'
var VueEvent = new Vue();
export default VueEvent;
<template>
<div id="home">
  <button @click="sendMsg()">我來觸發(fā)事件</button>
</div>
</template>
<script>
import VueEvent from "../models/VueEvent.js";
export default {
 data() {
  return {
   msg: "我是Home的msg"
  };
 },
 methods: {
  sendMsg() {
   VueEvent.$emit("tonews", this.msg);
  }
 }
};
</script>
<style>
</style>
<template>
<div id="news">
  我來接受事件--{{msg}}
</div>
</template>
<script>
import VueEvent from "../models/VueEvent.js";
export default {
 data() {
  return {
   msg: "我是News的msg"
  };
 },
 mounted() {
  VueEvent.$on("tonews", res => {
   this.msg = res;
  });
 }
};
</script>
<style>
</style>
<template>
 <div id="app">
  <v-home></v-home>
  <hr />
  <v-news></v-news>
 </div>
</template>

<script>
import Home from "./components/Home.vue";
import News from "./components/News.vue";
export default {
 name: "app",
 data() {
  return {
   msg: "Welcome to Your Vue.js App"
  };
 },
 components: {
  "v-home": Home,
  "v-news": News
 }
};
</script>

<style lang="scss">
</style>

以上這篇Vue執(zhí)行方法,方法獲取data值,設(shè)置data值,方法傳值操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue-router vuex-oidc動態(tài)路由實例及功能詳解

    vue-router vuex-oidc動態(tài)路由實例及功能詳解

    這篇文章主要為大家介紹了vue-router vuex-oidc動態(tài)路由實例及功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • Vue3中進行二維碼的生成與解碼實現(xiàn)詳解

    Vue3中進行二維碼的生成與解碼實現(xiàn)詳解

    這篇文章主要為大家介紹了Vue3中進行二維碼的生成與解碼實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • vant如何實現(xiàn)Collapse折疊面板標(biāo)題自定義

    vant如何實現(xiàn)Collapse折疊面板標(biāo)題自定義

    這篇文章主要介紹了vant如何實現(xiàn)Collapse折疊面板標(biāo)題自定義,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue在頁面和方法中如何通過遍歷對象獲取對象的鍵(key)和值(value)

    vue在頁面和方法中如何通過遍歷對象獲取對象的鍵(key)和值(value)

    這篇文章主要介紹了vue在頁面和方法中如何通過遍歷對象獲取對象的鍵(key)和值(value)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 詳解el Cascader懶加載數(shù)據(jù)回顯示例

    詳解el Cascader懶加載數(shù)據(jù)回顯示例

    這篇文章主要為大家介紹了詳解el Cascader懶加載數(shù)據(jù)回顯示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Vite結(jié)合whistle實現(xiàn)一勞永逸開發(fā)環(huán)境代理方案

    Vite結(jié)合whistle實現(xiàn)一勞永逸開發(fā)環(huán)境代理方案

    這篇文章主要為大家介紹了Vite結(jié)合whistle實現(xiàn)一勞永逸開發(fā)環(huán)境代理方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • Vue Components 數(shù)字鍵盤的實現(xiàn)

    Vue Components 數(shù)字鍵盤的實現(xiàn)

    這篇文章主要介紹了Vue Components 數(shù)字鍵盤的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • vue遮罩層如何阻止?jié)L動

    vue遮罩層如何阻止?jié)L動

    這篇文章主要介紹了vue遮罩層如何阻止?jié)L動,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • vue單元格多列合并的實現(xiàn)

    vue單元格多列合并的實現(xiàn)

    這篇文章主要介紹了vue單元格多列合并的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • VUE使用echarts?5.0以上版本渲染器未導(dǎo)入錯誤問題

    VUE使用echarts?5.0以上版本渲染器未導(dǎo)入錯誤問題

    這篇文章主要介紹了VUE使用echarts?5.0以上版本渲染器未導(dǎo)入錯誤問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06

最新評論