data
里面,视图在template
中Model
数据→View
视图→Controller
控制器Model View ViewModel
与Vue 的对应:view
对应template
,vm
对应new Vue({…})
,model
对应data
view
可以通过事件绑定的方式影响 model
,model
可以通过数据绑定的形式影响到view
,viewModel
是把model
和 view
连起来的连接器步骤:
1.导入Vue.js
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
或
<script src="https://cdn.jsdelivr.net/npm/vue">script>
2.创建Vue实例对象,设置el属性和data属性
el:挂载点
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
})
</script>
3.使用简洁的模板语法把数据渲染在页面上
<div id="app">
{
{ message }}
div>
4.小结
完整代码:
<html>
<head>
<meta charset="utf-8" />
<title>title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
{
{ message }}
div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
})
script>
body>
html>
v-text
:不能解析标签v-html
:可以解析标签
<html>
<head>
<meta charset="utf-8" />
<title>title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<span v-text="text">span>
<span v-html="text">span>
div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
text: 'Hello'
}
})
script>
body>
html>
{ {data中的数据}}
v-text
和表达式的区别:
v-text
没有闪烁的问题,插值表达式有<span v-cloak>{
{msg}}span>
v-cloak
能够解决插值表达式闪烁的问题:它首先会把设置了v-cloak
的标签元素隐藏dispaly:none;
,当加载完毕之后,元素身上的v-cloak
就会消失。
<span v-show="false">{
{msg}}span>
v-show
:改变css样式中display
属性,true
表示block,false
表示none
绑定
<html>
<head>
<meta charset="utf-8" />
<title>title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<span v-bind:title="message">
鼠标悬停几秒钟查看此处动态绑定的提示信息!
span>
div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
})
script>
body>
html>
v-if
和v-else
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<h2 v-if="ok">YESh2>
<h2 v-else>NOh2>
div>
<script>
var vm = new Vue({
el: "#app",
data: {
ok: true
}
});
script>
body>
html>
v-if
、v-else-if
和v-else
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<h2 v-if="type==='A'">Ah2>
<h2 v-else-if="type==='B'">Bh2>
<h2 v-else>Ch2>
div>
<script>
var vm = new Vue({
el: "#app",
data: {
type: 'A'
}
});
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<li v-for="(item,index) in items">
{
{item.message}}---{
{index}}
li>
div>
<script>
var vm = new Vue({
el: "#app",
data: {
items: [
{
message: '简介'},
{
message: '数据'},
{
message: 'Hello'}
]
}
});
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<button v-on:click="sayHi">按钮button>
div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
message: '好好学习'
},
methods: {
//方法必须定义在Vue的Methods对象中
sayHi: function () {
alert(this.message);
}
}
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<input type="text" v-model="message">{
{message}}
div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
message: '123'
}
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
性别:
<input type="radio" name="radiosex" value="男" v-model="sex">
<input type="radio" name="radiosex" value="女" v-model="sex">
<p>
性别选择了:{
{sex}}
p>
div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
sex: ''
}
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
下拉框:
<select v-model="selected">
<option value="" disabled>--请选择--option>
<option>Aoption>
<option>Boption>
<option>Coption>
select>
<span>值:{
{selected}}span>
div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
selected: ''
}
})
script>
body>
html>
基本格式:
Vue.component("组件名",{
props: ['参数'],
template: '<li>{
{参数}}li>'
});
如果不需要传递参数,只是简单的组件复用,可以去掉props
要用 v-bind:参数名="data"
来绑定参数的值为data属性中的值
注意template:
中的容器只有一个父元素,不能有多个,可以用一个div包裹。
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<hello v-for="item in items" v-bind:hi="item" >hello>
div>
<script type="text/javascript">
Vue.component("hello",{
props: ['hi'],
template: '{
{hi}} '
});
var vm = new Vue({
el: '#app',
data: {
items: ['JAVA','HTML','PHP'],
}
})
script>
body>
html>
computed
会基于响应数据缓存,methods
不会缓存computed
的方法不会被执行,但methods
里面的方法会执行
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<p>当前时间:{
{currentTime()}}p>
<p>当前时间:{
{currentTime2}}p>
div>
<script type="text/javascript">
var vm = new Vue({
el: '#app',
data: {
msg: 'Hello'
},
methods: {
currentTime: function () {
return Date.now();//返回当前时间
}
},
computed: {
currentTime2: function () {
return Date.now();//返回当前时间
}
}
})
script>
body>
html>
表示,父组件可以在这个占位符中填充任何模板代码,如 HTML、组件等,填充的内容会替换子组件的
标签。
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<div>
<todo>
<slot1 slot="slot1" v-bind:item="item">slot1>
<slot2 slot="slot2" v-for="i in item2" v-bind:item2="i">slot2>
todo>
div>
div>
<script type="text/javascript">
Vue.component('todo',{
template: '' +
' ' +
''
+
' ' +
'' +
''
});
Vue.component('slot1',{
props: ['item'],
template: '{
{item}}
'
});
Vue.component('slot2',{
props: ['item2'],
template: '{
{item2}} '
});
var vm = new Vue({
el: '#app',
data: {
item: '课程',
item2: ['PHP','JAVA','JS']
}
})
script>
body>
html>
父组件使用props
传递数据给子组件,子组件通过自定义事件跟父组件通信。
this.$emit('自定义事件的名称',自定义事件处理程序需要的参数1,参数2,参数3);
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<div>
<todo>
<slot1 slot="slot1" v-bind:item="item">slot1>
<slot2 slot="slot2" v-for="(i,index) in item2" v-bind:item2="i"
v-bind:index="index" v-on:remove="removeItem(index)">slot2>
todo>
div>
div>
<script type="text/javascript">
Vue.component('todo',{
template: '' +
' ' +
''
+
' ' +
'' +
''
});
Vue.component('slot1',{
props: ['item'],
template: '{
{item}}
'
});
Vue.component('slot2',{
props: ['item2','index'],
//只能绑定当前组件的方法
template: '{
{index}}---{
{item2}} ',
methods: {
remove: function (index) {
this.$emit('remove',index);
}
}
});
var vm = new Vue({
el: '#app',
data: {
item: '课程',
item2: ['PHP','JAVA','JS']
},
methods: {
removeItem: function (index) {
console.log(this.item2[index]+"已删除");
this.item2.splice(index,1);//删除数组中的第index个
}
}
})
script>
body>
html>
官网:http://www.axios-js.com/
测试
(1)新建一个data.json数据:
{
"name":"高朗",
"url": "https://blog.csdn.net/qq_43466788",
"page": "1",
"isNonProfit":"true",
"address": {
"street": "含光门",
"city":"陕西西安",
"country": "中国"
},
"links": [
{
"name": "CSDN",
"url": "https://blog.csdn.net/"
},
{
"name": "4399",
"url": "https://www.4399.com/"
},
{
"name": "百度",
"url": "https://www.baidu.com/"
}
]
}
(2)
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.0/axios.js">script>
head>
<body>
<div id="app">
<span>{
{info.name}}span>
<a v-bind:href="info.url">博客地址a>
<span>{
{info.page}}span>
<span>{
{info.isNonProfit}}span>
<span>{
{info.address.country}}span>
<div v-for="(link,index) in info.links">
<p v-text="index">p>
<span v-text="link.name">span>
<a v-bind:href="link.url" v-text="link.url">a>
div>
div>
<script type="text/javascript">
var vm = new Vue({
el: '#app',
data(){
return{
info: {
//请求的返回参数合适,必须和 json字符串一样
name: null,
url: null,
page: null,
isNonProfit: null,
address: {
street: null,
city: null,
country: null
},
links: [
{
name: null,
url: null
},
{
name: null,
url: null
},
{
name: null,
url: null
}
]
},
}
},
mounted(){
//钩子函数 链式编程
axios.get('../data.json').then(response=>(this.info=response.data));
}
})
script>
body>
html>