vue的第八个功能:计算属性computed,他出版了几本书
<!DOCTYPE html><div id="computed-basics"><p>His published books:</p><span>{{ author.books.length > 2 ? 'Yes' :'No' }}</span></div></html><script s
·
<!DOCTYPE html>
<div id="computed-basics">
<p>His published books:</p>
<span>{{ author.books.length > 2 ? 'Yes' :'No' }}</span>
</div>
</html>
<script src="https://unpkg.com/vue@next"></script>
<script>
Vue.createApp({
data() {
return {
author: {
name: 'John Doe',
books: [
'Vue 2 - Advance Guide',
'Vue 3 - Basic Guide',
'Vue 4 - The Mystery'
]
}
}
},
computed: {
// 计算属性的 getter
publishedBooksMessage() {
// `this` 指向 vm 实例
return this.author.books.length > 0 ? 'Yes' : 'No'
}
}
}).mount('#computed-basics')
</script>
2可以改成0或3、4
<!DOCTYPE html>
<div id="computed-basics">
<p>His published books:</p>
<span>{{ author.books.length > 0 ? 'Yes' :'No' }}</span>
</div>
</html>
<script src="https://unpkg.com/vue@next"></script>
<script>
Vue.createApp({
data() {
return {
author: {
name: 'John Doe',
books: [
'Vue 2 - Advance Guide',
'Vue 3 - Basic Guide',
'Vue 4 - The Mystery'
]
}
}
},
computed: {
// 计算属性的 getter
publishedBooksMessage() {
// `this` 指向 vm 实例
return this.author.books.length > 0 ? 'Yes' : 'No'
}
}
}).mount('#computed-basics')
</script>
他出版了几本书
<!DOCTYPE html>
<div id="computed-basics">
<p>His published books:</p>
<span>{{ author.books.length }}</span>
</div>
</html>
<script src="https://unpkg.com/vue@next"></script>
<script>
Vue.createApp({
data() {
return {
author: {
name: 'John Doe',
books: [
'Vue 2 - Advance Guide',
'Vue 3 - Basic Guide',
'Vue 4 - The Mystery'
]
}
}
},
computed: {
// 计算属性的 getter
publishedBooksMessage() {
// `this` 指向 vm 实例
return this.author.books.length > 0 ? 'Yes' : 'No'
}
}
}).mount('#computed-basics')
</script>
第四个
<!DOCTYPE html>
<div id="computed-basics">
<p>His published books:</p>
<span>{{ publishedBooksMessage }}</span>
</div>
</html>
<script src="https://unpkg.com/vue@next"></script>
<script>
Vue.createApp({
data() {
return {
author: {
name: 'John Doe',
books: [
'Vue 2 - Advance Guide',
'Vue 3 - Basic Guide',
'Vue 4 - The Mystery'
]
}
}
},
computed: {
// 计算属性的 getter
publishedBooksMessage() {
// `this` 指向 vm 实例
return this.author.books.length > 0 ? 'Yes' : 'No'
}
}
}).mount('#computed-basics')
</script>
更多推荐
已为社区贡献10条内容
所有评论(0)