반응형
vue.js 2를 사용하여 스팬의 텍스트를 변경하는 방법
vue 컴포넌트는 다음과 같습니다.
<template>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
aria-expanded="false" :title="...">
...
<span v-if="totalNotif > 0" class="badge" id="total-notif">{{ totalNotif }}</span>
</a>
</li>
</template>
<script>
...
export default {
mounted() {
this.initialMount()
},
...
computed: {
...mapGetters([
'totalNotif'
])
},
methods: {
initialMount() {
Echo.private('App.User.' + window.Laravel.authUser.id).notification((notification) => {
const totalNotif = $('#total-notif').text()
const totalNotifNew = parseInt(totalNotif) + 1
$('#total-notif').text(totalNotifNew )
})
},
}
}
</script>
그건 효과가 있다.
그러나 jquery를 사용하여 스팬의 텍스트를 가져오고 업데이트합니다.
vue.js 2를 사용하여 하려면 어떻게 해야 하나요?
참고 문헌을 읽었는데watch
하지만 아직 사용하기에 혼란스럽다.
바꾸다
const totalNotif = $('#total-notif').text()
const totalNotifNew = parseInt(totalNotif) + 1
$('#total-notif').text(totalAllNew)
타고
//Set this.totalNotif to 1 if not exist or equals to 0. Else increment by 1
this.totalNotif = (this.totalNotif) ? (this.totalNotif + 1) : 1;
하지만 나는 코드를 이해할 수 없다.
computed: {
...mapGetters([
'totalNotif'
])
},
예를 들어 다음과 같은 것을 생각할 수 있습니다.
export default {
mounted() {
this.initialMount()
},
data() {
return {
totalNotif: 0
}
},
...
템플릿에서 다음과 같이 스팬을 변경합니다.
<span v-if="totalNotif > 0" class="badge" id="total-notif" v-text="totalNotif"></span>
접속할 수 있습니다.span
가치 있게totalNotif
VueJs 부분에서는 jquery 부분을 삭제하고 데이터만 업데이트합니다.totalNotif
스팬 텍스트 내용이 자동으로 업데이트됩니다.
편집 더 잘 이해하기 위해 스크립트 부분은 다음과 같습니다.
export default {
mounted() {
this.initialMount()
},
data() {
return {
totalNotif: 0
}
},
methods: {
initialMount() {
Echo.private('App.User.' + window.Laravel.authUser.id).notification((notification) => {
this.totalNotif = this.totalNotif + 1
})
},
}
}
언급URL : https://stackoverflow.com/questions/43093231/how-to-change-text-on-the-span-use-vue-js-2
반응형
'source' 카테고리의 다른 글
값이 null이 아닌 경우 VueJ 표시 버튼 (0) | 2022.08.24 |
---|---|
Vue.js Functional 컴포넌트에서 이벤트를 내보내는 방법 (0) | 2022.08.24 |
*, /, +, -, % 연산자를 사용하지 않고 숫자를 3으로 나눕니다. (0) | 2022.08.21 |
Java 가비지 컬렉션은 순환 참조와 함께 어떻게 작동합니까? (0) | 2022.08.21 |
문자열 끝 - char c=0 vs char c='\0' (0) | 2022.08.21 |