vue零基础开发015——ref引用

2019-11-22  本文已影响0人  文朝明

【ref模型】

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>组件中ref引用</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="root">
        <div ref="hello" @click="handleClick">ref引用</div>
    </div>
    <script>
        var vm = new Vue({
            el: "#root",
            methods: {
                handleClick: function () {
                    //vue中所有引用中叫做hello的引用
                    console.log(this.$refs.hello.innerHTML)

                }
            }
        })
    </script>
</body>
</html>
引用ref

【dom操作】

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>组件中ref引用</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="root">
        <counter></counter>
        <counter></counter>
    </div>
    <script>
        Vue.component('counter', {
            template: '<div @click="handleClick">{{number}}</div>',
            data: function () {
                return {
                    number:0
                }
            },
            methods: {
                handleClick: function () {
                    this.number++
                }
            }
        })
        var vm = new Vue({
            el:'#root'
        })
    </script>
</body>
</html>
初始化
点击数字+1

【ref引用计算】

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
 <head>  
 <meta charset="utf-8" />  
 <title>组件中ref引用</title>  
 <script src="./vue.js"></script>  
 </head>  
 <body>  
 <div id="root">  
 <counter ref="one" @change="handleChange"></counter>  
 <counter ref="two" @change="handleChange"></counter>  
 <div>{{total}}</div>  
 </div>  
 <script>  
 Vue.component('counter', {  
 template: '<div @click="handleClick">{{number}}</div>',  
 data: function () {  
 return {  
 number:0  
 }  
 },  
 methods: {  
 handleClick: function () {  
 this.number++  
 this.$emit('change')  
 }  
 }  
 })  
 var vm = new Vue({  
 el: '#root',  
 data: {  
 total:0  
 },  
 methods: {  
 handleChange: function () {  
 // console.log("change")  
 // console.log(this.$refs.one.number)  
 this.total=this.$refs.one.number+ this.$refs.two.number  
   }  
 }  
 })  
 </script>  
 </body>  
 </html> 

计算
上一篇下一篇

猜你喜欢

热点阅读