Vue.js(2.x)之插值

看了一些友邻写的文章,不少是基于1.0版本的,有些东西在2.x版本应该已经废除了(如属性插值、单次插值在2.x版本上运行根本不执行)。对于不理解的东东,找起资料来就更麻烦了。不得不老老实实线下测试并整理记录:

1.文本插值
官网文档的文字解释看的好没意思,不给栗子让人咋整:

v-once指令的用法应该与1.0版本的单次插值是一个意思。

完整示例如下图所示,Vue下的data中message里的内容只会被当做文本输出到页面,即使这样写也同样会直接输出,并不会被当作HTML标签解析:message: '<b>Hello Vue.js!</b>',页面输出就是:<b>Hello Vue.js!</b>。

2.纯 HTML
官网文档注释如下,看完一头雾水。照例找了个栗子出来看了看:

 

栗子代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
    <div v-html="message"></div>
    <div v-html="login"></div>
</div>
    
<script>
new Vue({
  el: '#app',
  data: {
    message: '<h1>菜鸟教程</h1>',
    login: '<input type="button" value="登录" >'  
  }
})
</script>
</body>
</html>
View Code

 

代码与页面效果对照着看:使用 v-html 指令用于输出 html 代码:直接在需要操作的元素上添加v-html属性,属性值为Vue数据data中定义的属性名。

 

再看下页面是怎么解析的,如下图:

到这里,使用Vue.js向HTML页面中插入HTML元素算是理解了。

3.属性

经过前面两个的理解,偶知道了使用Vue.js操作HTML元素的属性需要使用v-bind指令;但是不明白v-bind咋使用的,也不明白后面那句“这对布尔值的属性也有效 —— 如果条件被求值为 false 的话该属性会被移除”。照例找栗子看代码:

 查看栗子并测试后发现,v-bind指令可以用来设置元素的属性是否显示及显示后的属性值,以下图为例:

1)、如果data下的class1属性值设置为0或false,则div的class没有属性值,即为空;

2)、如果data下的class1属性值设置为1或true,则div的class值为class1;

3)、v-bind:class="{'类名class1':data中的class1}",这么设置元素的class是没问题的,可是改成其他的就不行,比如加一个设置id的,解析后代码却是这样的:

为毛class值正常,id值却显示的是[object Object]?(先留着明天再试试看:今天有小伙伴提醒看看关于JSON.stringify的知识。看到后面疑团解开了,如下面第二张图所示)


 

 

4.使用 JavaScript 表达式

还是先看一遍官网文档,依然需要栗子:

 

使用js表达式的插值方式如下图所示:

运行后页面显示:4 10 2;OK,这里疑问不大,到这里算是明白了:使用JavaScript表达式进行插值时只能使用单个有效的表达式才能生效。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Learn the fundamentals of vue.js by creating complex SPAs with Vuex, vue-router and more Key Features We bridge the gap between "learning" and "doing" by providing real-world examples that will improve your web development skills with Vue.js Explore the exciting features of Vue.js 2 through practical and interesting examples Explore modern development tools and learn how to utilize them by building applications with Vue.js Book Description Vue.js is a frontend web framework which makes it easy to do just about anything, from displaying data up to creating full-blown web apps, and has become a leading tool for web developers. This book puts Vue.js into a real-world context, guiding you through example projects that helps you build Vue.js applications from scratch. With this book, you will learn how to use Vue.js by creating three Single Page web applications. Throughout this book, we will cover the usage of Vue, for building web interfaces, Vuex, an official Vue plugin which makes caching and storing data easier, and Vue-router, a plugin for creating routes and URLs for your application. Starting with a JSON dataset, the first part of the book covers Vue objects and how to utilize each one. This will be covered by exploring different ways of displaying data from a JSON dataset. We will then move on to manipulating the data with filters and search and creating dynamic values. Next, you will see how easy it is to integrate remote data into an application by learning how to use the Dropbox API to display your Dropbox contents in an application In the final section, you will see how to build a product catalog and dynamic shopping cart using the Vue-router, giving you the building blocks of an e-commerce store. What you will learn Looping through data with Vue.js Searching and filtering data Using components to display data Getting a list of files using the dropbox API Navigating through a file tree and loading folders from a URL

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值