本文介绍了Vue.js中ref ($refs)用法举例总结,分享给大家,具体如下:
看Vue.js文档中的ref部分,自己总结了下ref的使用方法以便后面查阅。
一、ref使用在外面的组件上
HTML 部分
rush:xhtml;">
ref在外面的组件上
js部分
rush:js;">
var refoutsidecomponentTem={
template:""
};
var refoutsidecomponent=new Vue({
el:"#ref-outside-component",components:{
"component-father":refoutsidecomponentTem
},methods:{
consoleRef:function () {
console.log(this); // #ref-outside-component vue实例
console.log(this.$refs.outsideComponentRef); // div.childComp vue实例
}
}
});
我是子组件
二、ref使用在外面的元素上
HTML部分
rush:xhtml;">
omref">ref在外面的元素上
JS部分
rush:js;">
var refoutsidedomTem={
template:""
};
var refoutsidedom=new Vue({
el:"#ref-outside-dom",components:{
"component-father":refoutsidedomTem
},methods:{
consoleRef:function () {
console.log(this); // #ref-outside-dom vue实例
console.log(this.$refs.outsideDomref); //
我是子组件
ref在外面的元素上
} } });三、ref使用在里面的元素上---局部注册组件
HTML部分
rush:xhtml;">
ref在里面的元素上
JS部分
rush:js;">
var refinsidedomTem={
template:"
" +
"
",methods:{
consoleRef:function () {
console.log(this); // div.childComp vue实例
console.log(this.$refs.insideDomref); // 我是子组件
" + "我是子组件
} } }; var refinsidedom=new Vue({ el:"#ref-inside-dom",components:{ "component-father":refinsidedomTem } });四、ref使用在里面的元素上---全局注册组件
HTML部分
rush:xhtml;">
JS部分
rush:js;">
Vue.component("ref-inside-dom-quanjv",{
template:"
el:"#ref-inside-dom-all"
});
" +
"" +
"
",methods:{
showinsideDomref:function () {
console.log(this); //这里的this其实还是div.insideFather
console.log(this.$refs.insideDomrefAll); // var refinsidedomall=new Vue({ref在里面的元素上--全局注册
" + "el:"#ref-inside-dom-all"
});
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。