开心档之 Vue3
目录
Vue.js 事件处理器
Vue.js 实例
事件监听可以使用 v-on 指令:
v-ondiv id="app"> button v-on:click="counter += 1"> 增加 1/button> p> 这个按钮被点击了 { { counter } } 次。/p> /div>
script> new Vue({ el: '#app',data: { counter: 0} } )/script> 通常情况下,我们需要使用一个方法来调用 JavaScript 方法。
v-on 可以接收一个定义的方法来调用。
v-ondiv id="app">
!-- greet
是在下面定义的方法名 -->
button v-on:click="greet">
Greet/button>
/div>
script>
var app = new Vue({
el: '#app',data: {
name: 'Vue.js'}
,// 在 methods
对象中定义方法 methods: {
greet: function (event) {
// this
在方法里指当前 Vue 实例 alert('Hello ' + this.name + '!')// event
是原生 DOM 事件 if (event) {
alert(event.target.tagName)}
}
}
}
)// 也可以用 JavaScript 直接调用方法 app.greet() // ->
'Hello Vue.js!'/script>
除了直接绑定到一个方法,也可以用内联 JavaScript 语句:
v-ondiv id="app"> button v-on:click="say('hi')"> Say hi/button> button v-on:click="say('what')"> Say what/button> /div>
script> new Vue({ el: '#app',methods: { say: function (message) { alert(message)} } } )/script> 事件修饰符 Vue.js 为 v-on 提供了事件修饰符来处理 DOM 事件细节,如:event.preventDefault() 或 event.stopPropagation()。
Vue.js 通过由点 . 表示的指令后缀来调用修饰符。
.stop - 阻止冒泡.prevent - 阻止默认事件.capture - 阻止捕获.self - 只监听触发该元素的事件.once - 只触发一次.left - 左键事件.right - 右键事件.middle - 中间滚轮事件
.enter.tab.delete (捕获 "删除" 和 "退格" 键).esc.space.up.down.left.right.ctrl.alt.shift.meta 实例
Vue.js 实例本章节为大家介绍几个 Vue.js 实例,通过实例练习来巩固学到的知识点。
导航菜单实例导航菜单创建一个简单的导航菜单:
div id="main">
!-- 激活的菜单样式为 active 类 --> !-- 为了阻止链接在点击时跳转,我们使用了 "prevent" 修饰符 (preventDefault 的简称)。 --> nav v-bind:class="active" v-on:click.prevent> !-- 当菜单上的链接被点击时,我们调用了 makeActive 方法, 该方法在 Vue 实例中创建。 --> a href="#" class="home" v-on:click="makeActive('home')"> Home/a> a href="#" class="projects" v-on:click="makeActive('projects')"> Projects/a> a href="#" class="services" v-on:click="makeActive('services')"> Services/a> a href="#" class="contact" v-on:click="makeActive('contact')"> Contact/a> /nav> !-- 以下 "active" 变量会根据当前选中的值来自动变换 --> p> 您选择了 b> { { active} } 菜单/b> /p>
/div>
script> // 创建一个新的 Vue 实例 var demo = new Vue({ // DOM 元素,挂载视图模型 el: '#main',
// 定义属性,并设置初始值 data: { active: 'home' } , // 点击菜单使用的函数 methods: { makeActive: function(item){ // 模型改变,视图会自动更新 this.active = item; } }
} ); /script> 编辑文本实例文本编辑点击指定文本编辑内容:
div id="main" v-cloak v-on:click="hideTooltip">
!-- 这是一个提示框 v-on:click.stop 是一个点击事件处理器,stop 修饰符用于阻止事件传递 v-if 用来判断 show_tooltip 为 true 时才显示 --> div class="tooltip" v-on:click.stop v-if="show_tooltip"> !-- v-model 绑定了文本域的内容 在文本域内容改变时,对应的变量也会实时改变 --> input type="text" v-model="text_content" /> /div> !-- 点击后调用 "toggleTooltip" 方法并阻止事件传递 --> !-- "text_content" 变量根据文本域内容的变化而变化 --> p v-on:click.stop="toggleTooltip"> { { text_content} } /p>
/div>
script> var demo = new Vue({ el: '#main',data: { show_tooltip: false,text_content: '点我,并编辑内容。'} ,methods: { hideTooltip: function(){ // 在模型改变时,视图也会自动更新 this.show_tooltip = false; } ,toggleTooltip: function(){ this.show_tooltip = !this.show_tooltip; } } } )/script> 订单列表实例订单列表创建一个订单列表,并计算总价:
form id="main" v-cloak>
h1> Services/h1> ul> !-- 循环输出 services 数组, 设置选项点击后的样式 --> li v-for="service in services" v-on:click="toggleActive(service)" v-bind:class="{ 'active': service.active} "> !-- 显示订单中的服务名,价格 Vue.js 定义了货币过滤器,用于格式化价格 --> { { service.name} } span> { { service.price | currency} } /span> /li> /ul> div class="total"> !-- 计算所有服务的价格,并格式化货币 --> Total: span> { { total() | currency} } /span> /div>
/form> script>
// 自定义过滤器 "currency"Vue.filter('currency', function (value) { return '$' + value.toFixed(2); } );
var demo = new Vue({ el: '#main',data: { // 定义模型属性// 视图将循环输出数组的数据 services: [{ name: 'Web Development',price: 300,active:true} ,{ name: 'Design',price: 400,active:false} ,{ name: 'Integration',price: 250,active:false} ,{ name: 'Training',price: 220,active:false} ]} ,methods: { toggleActive: function(s){ s.active = !s.active; } ,total: function(){
var total = 0; this.services.forEach(function(s){ if (s.active){ total+= s.price; } } ); return total; } }
} );
/script> 搜索页面实例搜索页面在输入框输入搜索内容,列表显示配置的列表:
form id="main" v-cloak>
div class="bar"> !-- searchString 模型与文本域创建绑定 --> input type="text" v-model="searchString" placeholder="输入搜索内容" /> /div> ul> !-- 循环输出数据 --> li v-for="article in filteredArticles"> a v-bind:href="article.url"> img v-bind:src="article.image" /> /a> p> { { article.title} } /p> /li> /ul>
/form> script> var demo = new Vue({ el: '#main',data: { searchString: "",
// 数据模型,实际环境你可以根据 Ajax 来获取 articles: [ { "title": "What You Need To Know About CSS Variables", "url": "https://www.kxdang.com/topic//css/css-tutorial.html", "image": "https://static.kxdang.com/images/icon/css.png" } , { "title": "Freebie: 4 Great Looking Pricing Tables", "url": "https://www.kxdang.com/topic//html/html-tutorial.html", "image": "https://static.kxdang.com/images/icon/html.png" } , { "title": "20 Interesting JavaScript and CSS Libraries for February 2016", "url": "https://www.kxdang.com/topic//css3/css3-tutorial.html", "image": "https://static.kxdang.com/images/icon/css3.png" } , { "title": "Quick Tip: The Easiest Way To Make Responsive Headers", "url": "https://www.kxdang.com/topic//css3/css3-tutorial.html", "image": "https://static.kxdang.com/images/icon/css3.png" } , { "title": "Learn SQL In 20 Minutes", "url": "https://www.kxdang.com/topic//sql/sql-tutorial.html", "image": "https://static.kxdang.com/images/icon/sql.png" } , { "title": "Creating Your First Desktop App With HTML, JS and Electron", "url": "https://www.kxdang.com/topic//js/js-tutorial.html", "image": "https://static.kxdang.com/images/icon/html.png" } ] } , computed: { // 计算属性,匹配搜索 filteredArticles: function () { var articles_array = this.articles, searchString = this.searchString; if(!searchString){ return articles_array; } searchString = searchString.trim().toLowerCase(); articles_array = articles_array.filter(function(item){ if(item.title.toLowerCase().indexOf(searchString) !== -1){ return item; } } ) // 返回过滤后的数据 return articles_array; ; } }
} ); /script> 切换不同布局实例切换不同布局点击右上角的按钮来切换不同页面布局:
form id="main" v-cloak>
div class="bar"> !-- 两个按钮用于切换不同的列表布局 --> a class="list-icon" v-bind:class="{ 'active': layout == 'list'} " v-on:click="layout = 'list'"> /a> a class="grid-icon" v-bind:class="{ 'active': layout == 'grid'} " v-on:click="layout = 'grid'"> /a> /div> !-- 我们设置了两套布局页面。使用哪套依赖于 "layout" 绑定 --> ul v-if="layout == 'grid'" class="grid"> !-- 使用大图,没有文本 --> li v-for="a in articles"> a v-bind:href="a.url" target="_blank" rel="noopener noreferrer"> img v-bind:src="a.image.large" /> /a> /li> /ul> ul v-if="layout == 'list'" class="list"> !-- 使用小图及标题 --> li v-for="a in articles"> a v-bind:href="a.url" target="_blank" rel="noopener noreferrer"> img v-bind:src="a.image.small" /> /a> p> { { a.title} } /p> /li> /ul>
/form> script>
var demo = new Vue({ el: '#main', data: { // 视图模型,可能的值是 "grid" 或 "list"。 layout: 'grid', articles: [{ "title": "HTML 教程", "url": "https://www.kxdang.com/topic//html/html-tutorial.html", "image": { "large": "https://static.kxdang.com/images/mix/htmlbig.png", "small": "https://static.kxdang.com/images/icon/html.png" } } , { "title": "CSS 教程", "url": "https://www.kxdang.com/topic//css/css-tutorial.html", "image": { "large": "https://static.kxdang.com/images/mix/cssbig.png", "small": "https://static.kxdang.com/images/icon/css.png" } } , { "title": "JS 教程", "url": "https://www.kxdang.com/topic//js/js-tutorial.html", "image": { "large": "https://static.kxdang.com/images/mix/jsbig.jpeg", "small": "https://static.kxdang.com/images/icon/js.png" } } , { "title": "SQL 教程", "url": "https://www.kxdang.com/topic//sql/sql-tutorial.html", "image": { "large": "https://static.kxdang.com/images/mix/sqlbig.png", "small": "https://static.kxdang.com/images/icon/sql.png" } } , { "title": "Ajax 教程", "url": "https://www.kxdang.com/topic//ajax/ajax-tutorial.html", "image": { "large": "https://static.kxdang.com/images/mix/ajaxbig.png", "small": "https://static.kxdang.com/images/icon/ajax.png" } } , { "title": "Python 教程", "url": "https://www.kxdang.com/topic//pyhton/pyhton-tutorial.html", "image": { "large": "https://static.kxdang.com/images/mix/pythonbig.png", "small": "https://static.kxdang.com/images/icon/python.png" } } ] } } );
/script>
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: 开心档之 Vue3
本文地址: https://pptw.com/jishu/298649.html