- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
-
- head>
-
- <body>
- <div id="root"> div>
- body>
-
- <script>
- Vue.createApp({
- data() {
- return {
- count: 0
- }
- },
- methods: {
- handleBtnClick() {
- this.count = 0;
- }
- },
- mounted() {
- setInterval(() => {
- ++this.count;
- }, 1000/10);
- console.log("run mounted...");
- },
- template: `
- count is {{count}}
-
-
- `
- }).mount('#root');
- script>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
-
- head>
-
- <body>
- <div id="root" class="root"> div>
- body>
-
- <script>
- Vue.createApp({
- data() {
- return {
- count: 0
- }
- },
- methods: {
- handleBtnClick() {
- this.count = 0;
- }
- },
- mounted() {
- setInterval(() => {
- ++this.count;
- }, 1000/10);
- console.log("run mounted...");
- },
- template: `
- count is {{count}}
-
-
- `
- // }).mount('#root'); //id选择器
- // }).mount('.root'); //class选择器
- }).mount('div'); //标签选择器
- script>
-
- html>
v-text的作用是根据变量填充标签的内容。
示例代码:
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="app"> div>
- body>
-
- <script>
- Vue.createApp({
- data() {
- return {
- count: 0
- }
- },
- template: `
- <h3 v-text="'count is ' + count">h3>
- <h3 v-text="'count is ' + this.count">h3>
- <h3>count is {{count}} h3>
- <h3>count is {{this.count}} h3>
- `
- }).mount('#app');
- script>
-
- html>
运行结果:
v-html与v-text类似,作用是根据变量填充标签的html内容。
示例代码:
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="app"> div>
- body>
-
- <script>
- Vue.createApp({
- data() {
- return {
- content: '<b>hello world!b>'
- }
- },
- template: `
- <p v-html="'content is ' + content">p>
- <p v-html="'content is ' + this.content">p>
- <p>content is {{content}} p>
- <p>content is {{this.content}} p>
- `
- }).mount('#app');
- script>
-
- html>
运行结果:
v-on的作用是为元素绑定事件。
示例代码:
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="app"> div>
- body>
-
- <script>
- Vue.createApp({
- data() {
- return {
- content1: '',
- content2: '',
- content3: '',
- }
- },
- methods: {
- handleBtnClick() {
- this.content1 = 'handleBtnClick';
- },
- handleBtnDoubleClick() {
- this.content2 = 'handleBtnDoubleClick';
- },
- handleBtnMouseEnter() {
- this.content3 = 'handleBtnMouseEnter';
- },
- },
- template: `
- <p> {{content1}} p>
- <p> {{content2}} p>
- <p> {{content3}} p>
- <input type="button" value="鼠标单击事件" v-on:click="handleBtnClick"/>
- <input type="button" value="鼠标双击事件" v-on:dblclick="handleBtnDoubleClick"/>
- <input type="button" value="鼠标进入事件" v-on:mouseenter="handleBtnMouseEnter"/>
- `
- }).mount('#app');
- script>
-
- html>
v-on:也可简写成@
- <input type="button" value="鼠标单击事件" @click="handleBtnClick"/>
- <input type="button" value="鼠标双击事件" @dblclick="handleBtnDoubleClick"/>
- <input type="button" value="鼠标进入事件" @mouseenter="handleBtnMouseEnter"/>
成员函数定义 也可写成
- handleBtnClick: function() {
- this.content1 = 'handleBtnClick';
- },
- handleBtnDoubleClick: function() {
- this.content2 = 'handleBtnDoubleClick';
- },
- handleBtnMouseEnter: function() {
- this.content3 = 'handleBtnMouseEnter';
- },
运行结果:
v-show的作用是根据表达式的真假,切换元素的显示和隐藏。
示例代码:
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="app"> div>
- body>
-
- <script>
- Vue.createApp({
- data() {
- return {
- isShow: false,
- btnText: '显示',
- }
- },
- methods: {
- handleBtnClick: function() {
- this.isShow = !this.isShow;
- this.btnText = !this.isShow ? '显示' : '隐藏';
- },
- },
- template: `
-
hello world
-
- `
- }).mount('#app');
- script>
-
- html>
运行结果:
v-if的作用是根据表达式的真假,切换元素的显示和隐藏(操纵dom元素)。
示例代码:
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="app"> div>
- body>
-
- <script>
- Vue.createApp({
- data() {
- return {
- isShow: false,
- btnText: '显示',
- }
- },
- methods: {
- handleBtnClick: function() {
- this.isShow = !this.isShow;
- this.btnText = !this.isShow ? '显示' : '隐藏';
- },
- },
- template: `
-
hello world
-
test
-
-
- `
- }).mount('#app');
- script>
-
- html>
运行结果:
v-bind的作用是设置元素的属性。
示例代码:
- DOCTYPE html>
-
-
-
-
-
hello world -
-
- .active {
- border: 10px solid red;
- }
-
-
-
-
- Vue.createApp({
- data() {
- return {
- imgSrc: 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png',
- imgTitle: '百度',
- isActive: false
- }
- },
- template: `
-
- :class="isActive ? 'active' : ''" @click="toggleActive"/>
- `,
- methods: {
- toggleActive: function() {
- this.isActive = !this.isActive;
- }
- },
- }).mount('#app');
-
- 上述“”也可以简写成“”;
- 上述“:class="isActive ? 'active' : ''"”也可写成“:class="{active : isActive}"” 。
运行结果:
3.3 列表循环,表单元素绑定
v-for的作用是根据数据生成列表结构。
示例代码:
- DOCTYPE html>
-
-
-
-
-
hello world -
-
- .active {
- border: 10px solid red;
- }
-
-
-
-
- Vue.createApp({
- data() {
- return {
- arr: ['鸡蛋', '火腿', '西瓜', '香菜', '芒果'],
- }
- },
- template: `
-
-
- <{{item}} : {{index}}> - hello world
-
- `,
- }).mount('#app');
-
运行结果:
v-on还可以用于绑定带有参数的事件函数。
示例代码:
- DOCTYPE html>
-
-
-
-
-
hello world -
-
- .active {
- border: 10px solid red;
- }
-
-
-
-
- Vue.createApp({
- data() {
- return {
- arr: ['鸡蛋', '火腿', '西瓜', '香菜', '芒果'],
- }
- },
- template: `
-
-
-
-
- [{{item}} : {{index}}] - hello world
-
- `,
- methods: {
- add: function(item) {
- this.arr.push(item);
- },
- send: function() {
- let itemValue = document.getElementById('editCtl').value;
- this.add(itemValue);
- }
- },
- }).mount('#app');
-
运行结果:
v-model的作用是获取和设置表单元素的值(双向数据绑定)。
示例代码:
- DOCTYPE html>
-
-
-
-
-
hello world -
-
- .active {
- border: 10px solid red;
- }
-
-
-
-
- Vue.createApp({
- data() {
- return {
- arr: ['鸡蛋', '火腿', '西瓜', '香菜', '芒果'],
- itemValue: '哈密瓜',
- }
- },
- template: `
-
-
-
-
- [{{item}} : {{index}}] - hello world
-
- `,
- methods: {
- add: function(item) {
- this.arr.push(item);
- },
- send: function() {
- let itemValue = document.getElementById('editCtl').value;
- this.add(itemValue);
- }
- },
- }).mount('#app');
-
运行结果: