黑马程序员4小时入门VUE传送门
Vue
是一个操作JavaScript
的框架,类似于jQuery
,但比jQuery
好用,是现在的主流
DOCTYPE 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>Documentstitle>
head>
<body>
<div id="app">{{message}}div>
<script src="../Resources/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
message: "sss "
},
});
script>
body>
html>
el
挂载点<div id="app" class="app">{{message}}div>
<script>
var app = new Vue({
// .app对应的是class选择器,div对应的是标签选择器,el都可以挂载
el: "#app", // 此处是el挂载点,#对应的是id选择器,因此data会出现在id为app的组件中
data: {
message: "sss ",
}
});
script>
1. Vue
实例的作用范围是什么呢?
Vue
会管理el
选项命中的元素及其内部的后代元素【即可以嵌套发挥作用】
2. 是否可以使用其他的选择器?
可以使用其他的选择器,但是建议使用
ID
选择器
3. 是否可以设置其他的dom
元素呢?
可以使用其他的双标签,不能使用
HTML
和BODY
data
数据对象<div id="app">
<h1>{{message}}h1>
<h1>{{people.name}}h1>
<h1>{{children[0]}}h1>
div>
<script>
var app = new Vue({
el: "#app",
// data内部可以放字符串,键值对,数组等
data: {
message: "Hello,World!",
people:{
name:"Melon",
mobile:"123"
},
children:["a","b","c"]
},
});
script>
Vue
中用到的数据定义在data
中data
中可以写复杂类型的数据js
的语法即可Vue
指令v-text
指令
v-text
指令的作用是设置标签的内容,使用此标签会覆盖标签内原本的内容
<div id="app">
<h1>{{message+'哈哈'}}h1>
<h1 v-text="message">Hello,World!h1>
<h1 v-text="message+'呀'">h1>
div>
<script>
var app = new Vue({
el: "#app",
data: {
message: "你好!"
},
});
script>
v-html
指令其大致与
v-text
一致,不过在引用部分有html
语法时会被解析
<div id="app">
<h1 v-text="message">h1>
<h1 v-html="message">h1>
div>
<script>
var app = new Vue({
el: "#app",
data: {
message: "百度"
},
});
script>
v-on
指令
v-on
指令用于操作事件,@
符号等价于v-on:
<div id="app">
<input type="button" value="核武发射1" v-on:click="FBIWarnning" />
<input type="button" value="核武发射2" @click="FBIWarnning" />
<input type="button" value="双击发射" @dblclick="FBIWarnning" />
<h1 @click="changeFood">{{food}}h1>
div>
<script>
var app = new Vue({
el: "#app",
data: {
food: "黯然销魂饭",
},
// method和data一样是关键字,内放函数
methods: {
FBIWarnning: function () {
alert("一级戒备!!!!");
},
changeFood: function () {
this.food += "不咋地";
},
},
});
script>
多次点击“黯然销魂饭”会进行字符串的拼接
DOCTYPE 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>Documentstitle>
head>
<body>
<div id="app">
<input type="button" value="点击" @click="doIt('哎呀','妈呀!')">
<input type="text" @keyup.enter="sayHi">
div>
<script src="../Resources/vue.js">script>
<script>
var app = new Vue({
el: "#app",
methods:{
// 传入的两个参数
doIt:function(p1,p2){
alert(p1)
alert(p2)
},
sayHi:function(){
alert("吃了没")
}
}
});
script>
body>
html>
DOCTYPE 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>Documentstitle>
head>
<body>
<div id="app">
<div class="input-num">
<button @click="sub">-button>
<span>{{num}}span>
<button @click="add">+button>
div>
div>
<script src="../Resources/vue.js">script>
<script>
var app = 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>
body>
html>
v-show
指令为
true
时当前组件显示,为false
时当前组件隐藏
DOCTYPE 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>Documentstitle>
head>
<body>
<div id="app">
<input type="button" value="切换显示状态" @click="changeIsShow">
<input type="button" value="累加年龄" @click="addAge">
<img v-show="isShow" src="../Resources/good.jpg">
<img v-show="age>=18" src="../Resources/good.jpg">
div>
<script src="../Resources/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
isShow:false,
age:17
},
methods:{
changeIsShow:function(){
// 每次取反
this.isShow=!this.isShow;
},
addAge:function(){
this.age++;
}
}
});
script>
body>
html>
v-if
指令用法与
v-show
类似,不过其是直接操作DOM
元素【即为false
时,整体预计都会消失而不是只修改display
样式】
v-bind
指令v-bind
指令的作用是:为元素绑定属性v-bind
:属性名v-bind
,只保留:属性名DOCTYPE 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>Documentstitle>
<style>
/* 自定义样式,.开头是类样式 */
.active {
border: 1px solid red;
}
style>
head>
<body>
<div id="app">
<img v-bind:src="imgSrc" alt="" width="150" height="150" />
<br />
<img :src="imgSrc" alt="" width="150" height="150" />
<br />
<img
:src="imgSrc"
alt=""
:title="imgTitle+'123'"
:class="{active:isActive}"
@click="toggleActive"
width="150"
height="150"
/>
div>
<script src="../Resources/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
imgSrc: "../Resources/good.jpg",
imgTitle: "v-bind测试",
isActive: false,
},
methods: {
toggleActive: function () {
this.isActive = !this.isActive;
},
},
});
script>
body>
html>
v-for
指令v-for
指令的作用是:根据数据生成列表结构v-for
结合使用( item,index ) in数据
item
和index
可以结合其他指令一起使用DOCTYPE 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>Documentstitle>
head>
<body>
<div id="app">
<ul>
<li v-for="(it,index) in arr">一线城市{{index+1}}:{{it}}li>
ul>
<h2 v-for="item in meat" :title="item.name">
{{item.name}}
h2>
div>
<script src="../Resources/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
arr: ["北京", "上海", "广州", "深圳"],
meat: [{ name: "牛肉" }, { name: "猪肉" }],
},
});
script>
body>
html>
v-model
指令的作用是便捷的设置s和获取表单元素的值DOCTYPE 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>Documentstitle>
head>
<body>
<div id="app">
<input type="text" v-model="message" />
<h2>{{message}}h2>
div>
<script src="../Resources/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
message: "哈哈",
},
});
script>
body>
html>
axios
是一个专注于网络请求的库
DOCTYPE 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>Documentstitle>
head>
<body>
<div id="app">
<input type="button" value="获取笑话" @click="getJoke" />
<br>
<h3>{{joke}}h3>
div>
<script src="../Resources/vue.js">script>
<script src="../Resources/axios.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
joke: "很好笑的笑话",
},
methods: {
getJoke: function () {
var that = this;
// 向网站发送get请求【当然也有post请求,主要看接口要求】
// then之后第一个function用来处理请求成功的情况,
// 第2个function用来处理请求失败的情况
axios.get("https://autumnfish.cn/api/joke").then(
function (response) {
// 在axios中this不再指向Vue对象,因此若要使用需要提前存储变量
// response.data即从网站中获取的数据,不过需要哪部分数据还是得看网页数据得组成结构【类似于json,也可在Chrom利用F12查看】
that.joke=response.data
},
function (err) {}
);
},
},
});
script>
body>
html>