admin管理员组文章数量:1296897
I'm trying to create simple app with Vuejs that will check length of title tag (px).
Inside mounted I set the default value for title and run check length of default title tag but it return 0. When I change value of input, it work fine.
Here is my code. Link:
HTML:
<div id="app">
<input v-model="title" @input="titleCheck()">
<div id="title-preview">{{title}}</div>
<div class="message">{{message}}</div>
</div>
CSS:
#title-preview {
display: inline-block;
}
JS:
new Vue({
el: '#app',
data: {
title: '',
message: ''
},
mounted: function() {
this.$nextTick(function () {
this.title = "Default Title";
var title_width = document.getElementById("title-preview").offsetWidth;
this.message = title_width + 'px';
});
},
methods: {
titleCheck: function() {
var title_width = document.getElementById("title-preview").offsetWidth;
this.message = title_width + 'px';
}
}
})
Can anyone help me correct it?
I'm trying to create simple app with Vuejs that will check length of title tag (px).
Inside mounted I set the default value for title and run check length of default title tag but it return 0. When I change value of input, it work fine.
Here is my code. Link: https://codepen.io/mrtienhp97/pen/LeOzGa
HTML:
<div id="app">
<input v-model="title" @input="titleCheck()">
<div id="title-preview">{{title}}</div>
<div class="message">{{message}}</div>
</div>
CSS:
#title-preview {
display: inline-block;
}
JS:
new Vue({
el: '#app',
data: {
title: '',
message: ''
},
mounted: function() {
this.$nextTick(function () {
this.title = "Default Title";
var title_width = document.getElementById("title-preview").offsetWidth;
this.message = title_width + 'px';
});
},
methods: {
titleCheck: function() {
var title_width = document.getElementById("title-preview").offsetWidth;
this.message = title_width + 'px';
}
}
})
Can anyone help me correct it?
Share Improve this question edited Jan 8, 2018 at 16:18 ChrisM 1,5866 gold badges22 silver badges29 bronze badges asked Jan 6, 2018 at 16:34 Tiến Nguyễn QuangTiến Nguyễn Quang 911 gold badge1 silver badge3 bronze badges 1- 1 The reason is that the whole mounted() is happening before the rendering of the page. You should delay it somehow. I'm still trying to figure out a good answer though. – Las Ten Commented Jan 6, 2018 at 16:59
1 Answer
Reset to default 5You had the right idea in the mounted
event to use the $nextTick
to make sure the view was updated before getting the width, but you want to do that every time by making it part of titleCheck
. Here's what you can change to get it working:
- Add a
ref
attribute totitle-preview
so you don't need to usedocument.getElementById
(nothing wrong with it, but I prefer to letVue
do that for me)
<div id="title-preview" ref="titleRef">{{title}}</div>
- Change
titleCheck
to use$nextTick
as well as the$refs
to gettitle-preview
:
titleCheck: function() {
this.$nextTick(function () {
var title_width = this.$refs.titleRef.offsetWidth;
this.message = title_width + 'px';
});
}
- You can simplify your
mounted
event to set the title and then call yourtitleCheck
method:
mounted: function() {
this.title = "Default Title";
this.titleCheck();
}
This should get you what you're looking for.
本文标签: javascriptAccess data inside mounted VueJSStack Overflow
版权声明:本文标题:javascript - Access data inside mounted VueJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741644046a2390083.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论