• 21.ref属性


    分析脚手架

    一.School.vue

    <template>
    	<div class="school">
    		<h2>学校名称:{{name}}</h2>
    		<h2>学校地址:{{address}}</h2>
    	</div>
    </template>
    
    <script>
    	export default {
    		name:'School',
    		data() {
    			return {
    				name:'尚硅谷',
    				address:'北京·昌平'
    			}
    		},
    	}
    </script>
    
    <style>
    	.school{
    		background-color: gray;
    	}
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    三.App.vue

    <template>
    	<div>
    		<h1 v-text="msg" ref="title"></h1>
    		<button ref="btn" @click="showDOM">点我输出上方的DOM元素</button>
    		<School ref="sch"/>
    	</div>
    </template>
    
    <script>
    /*
    ## ref
    
            1. 作用:用于给节点打标识
    
            2. 读取方式:this.$refs.xxxxxx
    
    ## ref 属性
    
    1. 被用来给元素或子组件注册引用信息(id的替代者)
    
    2. 应用在html标签上获取的是真实DOM元素,应用在组件标签上是组件实例对象(vc)
    
    3. 使用方式:
    
        1. 打标识:```

    .....

    ```或 `````` 2. 获取:```this.$refs.xxx */
    //引入School组件 import School from './components/School' export default { name:'App', components:{School}, data() { return { msg:'欢迎学习Vue!' } }, methods: { showDOM(){ console.log(this.$refs.title) //真实DOM元素 console.log(this.$refs.btn) //真实DOM元素 console.log(this.$refs.sch) //School组件的实例对象(vc) } }, } </script>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    四.main.js

    //引入Vue
    import Vue from 'vue'
    //引入App
    import App from './App.vue'
    //关闭Vue的生产提示
    Vue.config.productionTip = false
    
    //创建vm
    new Vue({
    	el:'#app',
    	render: h => h(App)
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    mybatis
    Python股票量化投资课学习记录1
    LeetCode算法心得——全排列(回溯型排列)
    Linux系统中安装Redis
    前端工程化精讲第二课 界面调试:热更新技术如何开着飞机修引擎?
    OSPF状态机+SPF算法
    C++ Primer学习笔记-----附录:标准库
    python考研志愿填报模拟系统vue
    Win11怎么添加日语输入法
    初识MySQL
  • 原文地址:https://blog.csdn.net/m0_59708269/article/details/127667404