远程CDN
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js">script>
本地
<script src="vue.min.js">script>
创建一个vuetest目录,并且在目录下创建 01_vue入门程序.html 文件。
代码编写步骤:
1、定义html,引入vue.js;
2、定义app div,此区域作为vue的接管区域;
3、定义Vue实例,接管app区域;
4、定义model(数据对象);
5、在app中展示数据。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vue入门title>
<script src="vue.min.js">script>
head>
<body>
<div id="app">
{{ name }} <br />
{{ school.name }} {{ school.mobile }}<br />
<ul>
<li>{{ names[0] }}li>
<li>{{ names[1] }}li>
<li>{{ names[2] }}li>
ul>
div>
body>
<script>
//3. 创建vue实例
var VM = new Vue({
//定义 Vue实例挂载的元素节点,表示vue接管该div
el: "#app",
//4.定义model模型数据对象
data: {
name: "Hello World!!",
school: {
name: "家里蹲大学",
mobile: "0100010110"
},
names: ["鲁智深", "林冲", "武松"]
}
});
script>
html>
根据官网的介绍,指令是带有 v- 前缀的特殊属性。通过指令来操作DOM元素。
<body>
<div id="app">
<h2>{{ message }}好好学习h2>
<h2 v-text="message">好好学习h2>
<h2 v-text="message+1">h2>
<h2 v-text="message+'abc'">h2>
div>
body>
<script>
var VM = new Vue({
el: "#app",
data: {
message: "Java程序员"
}
});
script>
<body>
<div id="app">
<h2 v-html="url">h2>
div>
body>
<script>
var VM = new Vue({
el: "#app",
data: {
url: "百度一下"
}
});
script>
<div id="app">
<input type="button" value="点击按钮" v-on:click="方法名" />
<input type="button" value="点击按钮" @click="方法名" />
div>
<script>
var VM = new Vue({
el: "#app", //通过methods ,专门存放Vue中的方法
methods: {
方法名: function() {
alert("123!");
}
}
});
script>
1)编码步骤
2)案例演示
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Documenttitle>
<link rel="stylesheet" href="css/inputNum.css" />
head>
<body>
<div id="app">
<div>
<input type="button" class="btn btn_plus" v-on:click="add" />
<span>{{ num }}span>
<input type="button" class="btn btn_minus" @click="sub" />
div>
div>
body>
<script src="js/vue.min.js">script>
<script>
var VM = new Vue({
el: "#app",
data: {
num: 1
},
methods: {
add: function() {
if (this.num < 10) {
this.num++;
} else {
alert("别点了!最大了!");
}
},
sub: function() {
if (this.num > 0) {
this.num--;
} else {
alert("别点了!最小了!");
}
}
}
});
script>
html>
3)案例总结
<body>
<div id="app">
<input type="button" value="切换状态" @click="changeShow" /><br />
<img v-show="isShow" src="img/car.gif" />
<img v-show="age > 18" src="img/car.gif" />
div>
body>
<script src="js/vue.min.js">script>
<script>
var VM = new Vue({
el: "#app",
data: {
isShow: true,
age: 18
},
methods: {
changeShow: function() {
this.isShow = !this.isShow;
}
}
});
script>
<body>
<div id="app">
<input type="button" value="切换状态" @click="changeShow" />
<img v-if="isShow" src="img/car.gif" />
div>
body>
<script src="js/vue.min.js">script>
<script>
var VM = new Vue({
el: "#app",
data: {
isShow: false
},
methods: {
changeShow: function() {
this.isShow = !this.isShow;
}
}
});
script>
v-bind:属性名 = 表达式
<body>
<div id="app">
<img src="img/car.jpg" />
<img v-bind:src="imgSrc" />
<img :src="imgSrc" :title="imgTitle" />
<div :style="{ fontSize: size+'px' }">v-bind指令div>
div>
body>
<script src="js/vue.min.js">script>
<script>
var VM = new Vue({
el: "#app",
data: {
imgSrc: "img/car.jpg",
imgTitle: "小汽车",
size: 50
}
});
script>
<body>
<div id="app">
<input type="button" value="添加数据" @click="add" />
<input type="button" value="移除数据" @click="remove" />
<ul>
<li v-for="(item, index) in arr">{{ index + 1 }}城市:{{ item }}li>
ul>
<h2 v-for="(p) in persons">
{{ p.name }}
h2>
div>
body>
<script src="js/vue.min.js">script>
<script>
var VM = new Vue({
el: "#app",
data: {
// 数组
arr: ["北京", "上海", "广州", "深圳"],
// 对象数组
persons: [{ name: "张飞" }, { name: "关羽" }, { name: "刘备" }]
},
methods: {
add: function() {
// 向数组添加元素 push
this.persons.push({ name: "曹操" });
},
remove: function() {
// 移除元素
this.persons.shift();
}
}
});
script>
<body>
<div id="app">
<input
type="button"
value="一键三连"
@click="showTime(666,'下次一定!')"
/>
<input type="text" @keyup.enter="hi" />
div>
body>
<script src="js/vue.min.js">script>
<script>
var VM = new Vue({
el: "#app",
data: {},
methods: {
showTime: function(p1, p2) {
console.log(p1);
console.log(p2);
},
hi: function() {
alert("你好吗?");
}
}
});
script>
<body>
<div id="app">
<h2>{{ name }}h2>
div>
body>
<script src="./js/vue.min.js">script>
<script>
//创建的vue实例,就是 VM ViewModel
var VM = new Vue({
el: "#app",
//data就是MVVM模式中的 Model
data: {
name: "hello"
}
});
script>
<body>
<div id="app">
<input type="button" value="修改message" @click="update" />
<input type="text" v-model="message" />
<input type="text" v-model="password" />
<h2>{{ message }}h2>
div>
body>
<script src="js/vue.min.js">script>
<script>
// VM 业务逻辑控制
var VM = new Vue({
el: "#app",
// Model 数据存储
data: {
message: "Hello World!",
password: 123
},
methods: {
update: function() {
this.message = "你好";
}
}
});
script>
步骤:
步骤:
步骤:
步骤:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>小黑记事本title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="googlebot" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="../css/index.css" />
head>
<body>
<section id="app">
<header class="header">
<h1>VUE记事本h1>
<input
autofocus="autofocus"
autocomplete="off"
placeholder="输入日程"
class="new-todo"
v-model="inputValue"
@keyup.enter="add"
/>
header>
<section class="main">
<ul class="listview">
<li class="todo" v-for="(item,index) in list">
<div class="view">
<span class="index">{{ index + 1 }}.span>
<label>{{ item }}label>
<button class="destroy" @click="remove(index)">button>
div>
li>
ul>
section>
<footer class="footer">
<span class="todo-count">
<strong>{{ list.length }}strong> items left
span>
<button class="clear-completed" @click="clear">
Clear
button>
footer>
section>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var VM = new Vue({
el: "#app",
data: {
list: ["写代码", "吃饭", "睡觉"],
inputValue: "反对内卷"
},
methods: {
// 新增日程方法
add: function() {
// 将用户输入的内容添加到list中
this.list.push(this.inputValue);
},
// 删除方法
remove: function(index) {
console.log(index);
// 使用 splice(元素的索引,删除几个)
this.list.splice(index, 1);
},
// 清空操作
clear: function() {
this.list = [];
}
}
});
script>
body>
html>