vue学习(一)页面传值篇

掌握2种基础的传值方式:

(1)由外向里传

(2)由里向外传

第一种:由外向里传

效果:

第二种:由里向外传

实现效果:

代码示例:

videolist.vue组件

/* eslint-disable handle-callback-err */
<template>
  <div class="video container mt-5">
  <div>状态:{{status}}</div>
  <div class="form-inline mb-3">
    <input type="txet" class="form-control mr-1" v-model="username">
    <button class="btn btn-primary" @click="updateUsername">更新房间号</button>
    <h6>(videolist组件的按钮)</h6>
  </div>
  </div>
</template>

<script>
export default {
  name: 'VideoList',
  data() {
    return {
      msg: 'Welcome to 视频页面vue',
      username: '80100'
    }
  },
  methods: {
    updateUsername() {
      const vm = this
      vm.$emit('pushNewName', vm.username)// 1.触发事件名称2.向外部传的变量值
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

App.vue页面

<template>
  <div id="app">
      <nav class="navbar navbar-toggleable-md navbar-light bg-faded" style="background-color: #e3f2fd;">
      <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <a class="navbar-brand" href="#">状态</a>
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <div class="navbar-text">
           (外部App.vue组件页面接收传过来的参数):{{ username }}
          </div>
      </div>
    </nav>
<router-view @pushNewName="getNewName"></router-view>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      username: ''
    }
  },
  methods: {
    changeStatus() {
      this.myStatus = this.status
    },
    getNewName(newName) {
      console.log('getNewName接到的参数:' + newName)
      const vm = this
      vm.username = newName
    }
  }

}
</script>

<style lang='scss'>
@import "~bootstrap/scss/bootstrap";
/* #app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
} */
</style>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我先来一碗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值