老莫的笔记本  
  
查看: 1188|回复: 0

vue mvvm 双向数据绑定 与 ref选中demo 节点

[复制链接]

662

主题

878

帖子

5145

积分

超级版主

Rank: 8Rank: 8

积分
5145
发表于 2018-11-15 23:06:13 | 显示全部楼层 |阅读模式
首先理解一下 什么是mvvm
M(改变模型)->V(从而改变视图)->M(从而改变模型)

VUE的双向数据绑定,必须在表单中使用  
使用 this.msg 获取 在data中的 msg值;
使用 ref 选定dome 节点 【ref的作用 和 id 类似】
  1. <template>
  2.   <div id="app">
  3.     <hr>
  4.       <!-- 最简单的数据绑定 -->
  5.       {{msg2}}
  6.     <hr>
  7.       <!-- html输出。利用v-html -->
  8.       <span v-html="laomo"></span>
  9.     <hr>
  10.     <!-- 双向数据绑定1 -->
  11.     <div action="">
  12.        <input type="text" v-model="msg2">
  13.        <button @click="getmsg()">点击获取值</button>
  14.        <button @click="setmsg()">点击设置值</button>
  15.     </div>
  16.     <!-- 双向数据绑定2 -->
  17.     <div>
  18.        <input type="text" ref="userinfo">
  19.        <button @click="getinfo()">点击获取信息</button>
  20.        <button @click="setinfo()">点击设置信息</button>
  21.     </div>
  22.     <HelloWorld msg="Welcome to Your Vue.js App" laomo="事实上"/> -->
  23.   </div>
  24. </template>
  25. <script>
  26. import HelloWorld from './components/HelloWorld.vue'
  27. function abc(){
  28.   alert(1)
  29. }
  30. export default {
  31.   
  32.   data(){     //业务逻辑定义的数据
  33.     return {
  34.        msg2:'哈哈哈'
  35.        ,laomo:'<h2>apppppp<h2>'
  36.        ,arr:['one','two','three']
  37.        ,lmtitle:'这是一个title的值'
  38.        ,seen:false
  39.        ,a:false
  40.     }
  41.    
  42.   }
  43.   ,methods:{        //存放VUE的方法
  44.     getmsg(){
  45.       alert(this.msg2)
  46.     }
  47.     ,setmsg(){
  48.       this.msg2='设置的值    }
  49.     // ref的作用主要是获取dome节点
  50.     ,getinfo(){
  51.       // 使用ref来获取表单数据 ref类似于 id
  52.       // this.$refs.userinfo 获取demo节点
  53.       console.log(this.$refs.userinfo)
  54.       alert(this.$refs.userinfo.value)
  55.     }
  56.     ,setinfo(){
  57.       this.$refs.userinfo.value ='这又是一个ref设置的值'
  58.     }
  59.   }
  60.   ,name: 'app',
  61.   components: {
  62.     HelloWorld
  63.   }
  64.   
  65. }
  66. </script>
  67. <style>
  68. #app {
  69.   font-family: 'Avenir', Helvetica, Arial, sans-serif;
  70.   -webkit-font-smoothing: antialiased;
  71.   -moz-osx-font-smoothing: grayscale;
  72.   text-align: center;
  73.   color: #2c3e50;
  74.   margin-top: 60px;
  75. }
  76. .red{
  77.   color:red;
  78. }
  79. .blue{
  80.   color:blue;
  81. }
  82. </style>
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表