<html>
<head>
<meta charset="utf-8" />
<title>快速入门title>
<script src="vue.js">script>
head>
<body>
<div id="app">
{{message}}
div>
<script>
new Vue({
el:'#app', //表示当前vue对象接管了div区域
data:{
message:'hello world' //注意不要写分号结尾
}
});
script>
body>
html>
使用Vue的js文件之前,记得先引用
//下面的两个表达式都可以
{{ number + 1 }}
{{ ok ? 'YES' : 'NO' }}
//下面的不会生效
<!-- 这是语句,不是表达式 -->
{{ var a = 1 }}
<!-- 流控制也不会生效,请使用三元表达式 -->
{{ if (ok) { return message } }}
<html>
<head>
<meta charset="utf-8" />
<title>v-bindtitle>
<script src="vue.js">script>
head>
<body>
<div id="app">
<font size="5" v-bind:color="ys1">Javafont>
<font size="5" :color="ys2">C++font>
<hr>
itcasta>
div>
<script>
new Vue({
el:'#app', //表示当前vue对象接管了div区域
data:{
ys1:"red",
ys2:"green",
id:1
}
});
script>
body>
html>
对于上述Vue代码,过程可以解析如下:
- 创建了一个Vue对象(根据Vue语法)
- 这个Vue对象一旦创建, 会立即检查 它的el属性,
- 他会根据el属性找到一个对应id的html代码
- 如果找到了, 把找到的html代码所对应的作用域 和 这个Vue对象’绑定起来’
- 这个html代码所对应的作用域 就不在仅仅是html代码作用域, 还是这个Vue对象作用域
- 这个作用域代码 会重新, 按照Vue语法再解析一边
- Vue固有属性
- el
- data
- computed
只能用于表单元素的value上
<html>
<head>
<meta charset="utf-8" />
<title>v-modeltitle>
<script src="vue.js">script>
head>
<body>
<div id="app">
姓名:<input type="text" id="username" v-model="user.username"><br>
密码:<input type="password" id="password" v-model="user.password"><br>
<input type="button" @click="fun" value="获取">
div>
<script>
new Vue({
el:'#app', //表示当前vue对象接管了div区域
data:{
user:{username:"",password:""}
},
methods:{
fun:function(){
alert(this.user.username+" "+this.user.password);
this.user.username="tom";
this.user.password="11111111";
}
}
});
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="vue.js">script>
head>
<body>
<div id="root">
{{num}}
<input v-bind:value="num">
<select v-model="num">
<option value="1">1option>
<option value="2">2option>
<option value="3">3option>
<option value="4">4option>
select>
<textarea v-model="num">textarea>
<input v-model:value="num">
<input v-model="num">
div>
<script>
new Vue({
el: "#root",
data: {
num: 2
}
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="vue.js">script>
head>
<body>
<div id="root">
<div v-text="obj.name">
div>
<div v-text="obj.age">
div>
<div v-html="obj.name">
div>
<div v-html="obj.age">
div>
div>
<script>
new Vue({
el: "#root",
data: {
obj:{
name: "zs",
age: 18
}
}
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="vue.js">script>
head>
<body>
<div id="root">
<div v-show="bool">
<img src="https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3363295869,2467511306&fm=26&gp=0.jpg">
div>
<button @click="changeimg">改变button
<button v-on:click="f">改变button>
<button @click="f"> 改变button>
div>
<script>
new Vue({
el: "#root",
data: {
bool: false
},
methods: {
f: function () {
// 一定要加this
this.bool = !this.bool
}
}
})
function f() {
alert('?')
}
script>
body>
html>
<html>
<head>
<meta charset="utf-8" />
<title>事件处理title>
<script src="vue,js">script>
head>
<body>
<div id="app">
div>
<script>
new Vue({
el:'#app', //表示当前vue对象接管了div区域
methods:{
fun:function(msg,event){
if(!((event.keyCode>=48&&event.keyCode<=57)||event.keyCode==8||event.keyCode==46)){
event.preventDefault();
}
}
}
});
script>
body>
html>
<html>
<head>
<meta charset="utf-8" />
<title>事件处理title>
<script src="vue.js">script>
head>
<body>
<div id="app">
<div v-on:mouseover="fun1" id="div">
<textarea v-on:mouseover="fun2($event)">这是一个文件域textarea>
div>
div>
<script>
new Vue({
el:'#app', //表示当前vue对象接管了div区域
methods:{
fun1:function(){
alert("div");
},
fun2:function(event){
alert("textarea");
event.stopPropagation();//阻止冒泡
}
}
});
script>
body>
html>
<html>
<head>
<meta charset="utf-8" />
<title>v-on 事件修饰符title>
<script src="vue.js">script>
head>
<body>
<div id="app">
<form @submit.prevent action="http://www.itcast.cn" method="get">
<input type="submit" value="提交">
form>
<div @click="fun">
<a @click.stop href="http://www.itcast.cn">itcasta>
div>
div>
<script>
new Vue({
el:'#app', //表示当前vue对象接管了div区域
methods:{
fun:function(){
alert("hello itcast");
}
}
});
script>
body>
html>
<html>
<head>
<meta charset="utf-8" />
<title>v-on 按钮修饰符title>
<script src="vue.js">script>
head>
<body>
<div id="app">
<input type="text" v-on:keyup.enter="fun">
div>
<script>
new Vue({
el:'#app', //表示当前vue对象接管了div区域
methods:{
fun:function(){
alert("你按了回车");
}
}
});
script>
body>
html>
<p>
<input @keyup.alt.67="clear">
<div @click.ctrl="doSomething">Do somethingdiv>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../vue.js">script>
head>
<body>
<div id="root">
<div v-for="(item, index) of arr" :key="index" @click="deletediv(xxx)">
{{item}}--{{index}}
div>
<input v-model="inputstr">
<button @click="add">添加button>
div>
<script>
new Vue({
el: "#root",
data: {
inputstr: '',
arr: ["zs", "ls", "wu", "zl"]
},
methods: {
//添加列表项目
add: function () {
this.arr.push(this.inputstr)
},
//点击某一项即可删除
deletediv: function (index) {
this.arr.splice(index, 1)
}
}
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../vue.js">script>
head>
<body>
<div id="root">
<div v-pre>
{{msg}}
div>
div>
<script>
new Vue({
el: "#root",
data: {
msg: "zs"
}
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../vue.js">script>
head>
<body>
<div id="root">
<div v-once>
{{msg}}
div>
{{msg}}
<input v-model="msg">
div>
<script>
new Vue({
el: "#root",
data: {
msg: "zs"
}
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../vue.js">script>
<style>
/*属性选择器*/
[v-cloak]{
/*display: none;*/
font-size: 100px;
}
style>
head>
<body>
<div id="root">
<div v-cloak>
{{msg}}
div>
div>
<script>
//10秒之后执行方法f()
setTimeout('f()', 10000)
function f() {
new Vue({
el: "#root",
data: {
msg: "zs"
}
})
}
script>
body>
html>
把Vue对象里的可以作为
字符串:{代码块}
写法的均看做属性(key-value性质),不是属性的在这里作为属性统一看待
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="vue.js">script>
head>
<body>
<div id="root">
{{sum}}
<br>
<input v-model="num1" ><br>
<input v-model="num2" >
<hr>
{{sum2}}
div>
<script>
new Vue({
el: "#root",
data: {
num1: 0,
num2: 0
},
computed: {
// 不是方法, 外在表现是一个属性
sum: function () {
// 计算属性, 是通过别的属性计算而来, 他是依赖于别的属性的
return parseInt(this.num1) + parseInt(this.num2)
},
sum2: function () {
return parseInt(this.num1) + parseInt(this.num2) + this.sum
}
}
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="vue.js">script>
head>
<body>
<div id="root">
{{sum}}<br>
<input v-model="num1">
<input v-model="num2">
div>
<script>
new Vue({
el: "#root",
data: {
sum: 0,
num1: 0,
num2: 0
},
watch: {// 侦听器: 侦听一个属性的改变, 然后触发一个方法
// 方法名, 就是要侦听的属性
num1: function () {
this.sum = parseInt(this.num1) + parseInt(this.num2)
},
num2: function () {
this.sum = parseInt(this.num1) + parseInt(this.num2)
}
}
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="vue.js">script>
head>
<body>
<div id="root">div>
<script>
// 以后html写代码, html只需要提供一个入口,
// html js 都可以在一个vue对象中实现
new Vue({
el: "#root",
data: {
msg: "123"
},
template:
//语法强制要求,template外层必须要有div标签包裹
"<div>
<div @click='clickdiv'>
{{msg}}
</div>" +
"<p @click='clickp'>
{{msg}}
</p>
</div>",
methods: {
clickdiv: function () {
this.msg = "div"
},
clickp: function () {
this.msg = "p"
}
}
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../vue.js">script>
<style>
td {
width: 200px;
height: 30px;
text-align: center;
}
[colspan] {
background: red;
}
div {
margin: 0 0 5px 0;
}
select {
height: 35px;
text-align: center;
}
button {
height: 33px;
}
input {
height: 30px;
}
style>
head>
<body>
<div id="root">
<div>
<select v-model="currency1" @change="getrate">
<option value="0">美元option>
<option value="1">人民币option>
<option value="2">欧元option>
<option value="3">日元option>
<option value="4">韩币option>
<option value="5">港币option>
select>
<button @click="changeselected">互换button>
<select v-model="currency2" @change="getrate">
<option value="0">美元option>
<option value="1">人民币option>
<option value="2">欧元option>
<option value="3">日元option>
<option value="4">韩币option>
<option value="5">港币option>
select>
数额:
<input v-model="num" @change="getrate">
保留小数:
<select v-model="point" @change="getrate">
<option value="0">0option>
<option value="1">1option>
<option value="2">2option>
<option value="3">3option>
<option value="4">4option>
<option value="5">5option>
<option value="6">6option>
select>
div>
<table border="1">
<tr>
<td colspan="3">按当前汇率换算结果td>
tr>
<tr>
<td>{{td11}}td>
<td>汇率td>
<td>{{td13}}td>
tr>
<tr>
<td id="td21">{{td21}}td>
<td id="td22">{{td22}}td>
<td id="td23">{{td23}}td>
tr>
table>
div>
<script>
new Vue({
el: "#root",
data: {
arrRate: [1, 6, 0.9, 100, 1000, 7],
currencies: ['美元', '人民币', '欧元', '日元', '韩币', '港币'],
currency1: 1,
currency2: 0,
num: 0,
td11: "人民币",
td13: "美元",
td21: 0,
td22: 0.1667,
td23: 0,
point:4
},
methods: {
changeselected: function () {
var mid = this.currency1;
this.currency1 = this.currency2;
this.currency2 = mid;
this.getrate();
},
getrate: function () {
this.td21 = this.num;
var num = this.num * this.arrRate[this.currency2] / this.arrRate[this.currency1];
this.td23 = num.toFixed(this.point);
this.td11 = this.currencies[this.currency1];
this.td13 = this.currencies[this.currency2];
var num = this.arrRate[this.currency2] / this.arrRate[this.currency1];
this.td22 = num.toFixed(this.point);
}
},
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="vue.js">script>
head>
<body>
<div id="root">
div>
<script>
var son1 = {
template:"son1",
methods: {
f: function () {
alert(123)
}
}
}
var son2 = {
template:"son2"
}
var son3 = {
template:"son3"
}
new Vue({
el: "#root",
data: {},
///Vue组件
components:{
//son1对象替换x标签
x:son1,
//son2对象替换y标签
y:son2,
//son3对象替换z标签
z:son3,
},
template:" ",
methods: {
f: function () {
alert(123)
}
}
})
script>
body>
html>
<html>
<head>
<meta charset="utf-8" />
<title>生命周期title>
<script src="js/vuejs-2.5.16.js">script>
head>
<body>
<div id="app">
{{message}}
div>
<script>
var vm = new Vue({
el: "#app",
data: {
message: 'hello world'
},
beforeCreate: function() {
console.log(this);
showData('创建vue实例前', this);
},
created: function() {
showData('创建vue实例后', this);
},
beforeMount: function() {
showData('挂载到dom前', this);
},
mounted: function() {
showData('挂载到dom后', this);
},
beforeUpdate: function() {
showData('数据变化更新前', this);
},
updated: function() {
showData('数据变化更新后', this);
},
beforeDestroy: function() {
vm.test = "3333";
showData('vue实例销毁前', this);
},
destroyed: function() {
showData('vue实例销毁后', this);
}
});
function realDom() {
console.log('真实dom结构:' + document.getElementById('app').innerHTML);
}
function showData(process, obj) {
console.log(process);
console.log('data 数据:' + obj.message)
console.log('挂载的对象:')
console.log(obj.$el)
realDom();
console.log('------------------')
console.log('------------------')
}
vm.message="good...";
vm.$destroy();
script>
body>
html>
http://www.nodejs.cn/
下载node.js安装包,打开msi安装包,会自动安装node.js和npm,
node -v
,npm -v
。查看是否出现版本号。npm install -g cnpm --registry=https://registry.npm.taobao.org
,安装cnpm,
cnpm -v
,查看是否出现版本号。cnpm install -g @vue/cli
,安装cli脚手架工具,卡住了则按ctrl+c,终止批处理操作,重新键入命令安装。
vue -V
(注意V大写) 出现版本号cnpm install -g @vue/cli-init
,安装cli桥接工具,等待即可cnpm install -g webpack
,安装webpack,vue init webpack vuetest
,其中vuetest是项目名称cnpm install -g
npm run dev
后,浏览器里键入地址localhost://8080即可也可以使用npm,配置国内镜像源
npm config set registry https://registry.npm.taobao.org
proxyTable{}
语句里可以配置代理assetsPublicPath: '/'
语句里的/要修改为./对于一个新创建的Vue项目,加载顺序为:
- 首先加载index.html项目入口文件,主要内容仅有一句代码
- 进入src目录下寻找main.js文件,找到了一个vue对象,id是与index.html的div标签绑定的
- main.js文件中,vue对象里找到了App标签,替换掉index.html中的div标签
- 接下来,继续加载App标签是由自己的子组件定义的,而子组件是路径导入的
- 找到App.vue文件及它的默认对象,它的模板继续替换掉main.js中的components: { App }组件,只不过模板是定义在顶部的
- 显示的内容就是template中的内容,其中的style标签就是他的css样式(id选择器)
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
//在同级目录的某个文件夹下,实际上导出的是App.vue的默认导出的对象
import App from './App'
//项目开发的主配置
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
//绑定了index.html入口文件的div标签
el: '#app',
//App子组件,该语句相当于 components:{App:App}, 前者App是标签,后者App是vue对象(文件),真正加载的实际上就是App.vue文件
components: { App },
//替换App标签
template: ' '
})
<template>
<div id="app">
<img src="./assets/logo.png">
<HelloWorld/>
div>
template>
<script>
import HelloWorld from './components/HelloWorld'
//默认导出,App.vue向外界暴露的就是这个默认导出的js对象
export default {
name: 'App',
components: {
HelloWorld
}
}
script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
style>
一定要记得在cmd里运行项目,才能在浏览器里localhost://8080查看到页面样式
对于这个简单的项目,vue组件树有如下的结构
app.vue
|
---------------------------------------------------------------
| | | | |
top1 top2 top3 body1 body2
|
--------------------------------
| | |
BodyLeft BodyCenter BodyRight
<template>
<div>top1div>
template>
<script>
export default {
created() {
// TODO: 加载数据: 后端
}
}
script>
<style scoped>
style>
<template>
<div>top2div>
template>
<script>
export default {
created() {
// TODO: 加载数据: 后端
}
}
script>
<style scoped>
style>
<template>
<div>top3div>
template>
<script>
export default {
created() {
// TODO: 加载数据: 后端
}
}
script>
<style scoped>
style>
<template>
<div>
<body-left class="bodyleft">body-left>
<body-center class="bodycenter">body-center>
<body-right class="bodyright">body-right>
div>
template>
<script>
import BodyLeft from "./body/BodyLeft";
import BodyCenter from "./body/BodyCenter";
import BodyRight from "./body/BodyRight";
export default {
components: {
BodyLeft,
BodyRight,
BodyCenter
},
created() {
// TODO: 加载数据: 后端
}
}
script>
<style scoped>
.bodyleft{
width: 200px;
height: 500px;
background: #16c050;
float: left;
}
.bodycenter{
width: 700px;
height: 500px;
margin-left: 50px;
background: #c0b446;
float: left;
}
.bodyright{
width: 200px;
height: 500px;
margin-left: 50px;
background: #c0323c;
float: left;
}
style>
<template>
<div>BodyLeftdiv>
template>
<script>
export default {
name: "BodyLeft"
}
script>
<style scoped>
style>
<template>
<div>BodyCenterdiv>
template>
<script>
export default {
name: "BodyCenter"
}
script>
<style scoped>
style>
<template>
<div>BodyRightdiv>
template>
<script>
export default {
name: "BodyRight"
}
script>
<style scoped>
style>
<template>
<div>body2div>
template>
<script>
export default {
created() {
// TODO: 加载数据: 后端
}
}
script>
<style scoped>
style>
<template>
<div class="app">
<top1 class="top1">top1>
<top2 class="top2">top2>
<top3 class="top3">top3>
<body1 class="body1">body1>
<body2 class="body2">body2>
div>
template>
<script>
//导入
import Top1 from './components/Top1'
import Top2 from './components/Top2'
import Top3 from './components/Top3'
import Body1 from './components/Body1'
import Body2 from './components/Body2'
export default {
name: 'App',
//注册组件
components: {
Top1: Top1,
Top2,
Top3,
Body1,
Body2
},
created() {
// TODO 请求参数比较合适
}
}
script>
<style>
.app{
width: 100%;
height: 1000px;
/*background: red;*/
}
.top1{
width: 100%;
height: 45px;
background: silver;
border-bottom: 1px solid;
}
.top2{
width: 100%;
height: 160px;
background: #c0323c;
}
.top3{
width: 100%;
height: 30px;
background: #16c050;
}
.body1{
width: 1200px;
height: 500px;
background: antiquewhite;
margin: 0 auto;
}
.body2{
width: 1200px;
height: 300px;
background: #1735fa;
margin: 0 auto;
}
style>
this.$emit("方法名",this.参数名)
<template>
<div>{{sonname}}div>
template>
<script>
export default {
name: "Son1",
//接收父组件传来的值
props: ['sonname']
}
script>
<style scoped>
style>
<template>
<div>{{sonage}}div>
template>
<script>
export default {
name: "Son2",
//接收父组件传来的值
props: ['sonage']
}
script>
<style scoped>
style>
<template>
<div id="app">
<son1 class="son1" v-bind:sonname="list[0].name">son1>
<son2 class="son2" :sonage="list[0].age">son2>
div>
template>
<script>
import Son1 from "./components/Son1";
import Son2 from "./components/Son2";
export default {
name: 'App',
data() {
return{
list: [{
name: "zs",
age: 18
},
{
name: "ls",
age: 20
}]
}
},
components: {
Son1,
Son2
}
}
script>
<style>
.son1{
width: 400px;
height: 400px;
border: 1px solid;
float: left;
}
.son2{
width: 500px;
height: 400px;
margin-left: 100px;
border: 1px solid;
float: left;
}
style>
这个例子,Son1抛出了参数给父组件App.vue,父组件App.vue再传值给子组件Son2
$属性的用途:为了说明这是个方法,而不是属性。目的是为了和data的属性区分开
<template>
<div>
<input v-model="inputstr"><button @click="add">添加button>
div>
template>
<script>
export default {
name: "Son1",
data(){
return{
//初始状态,inputstr为空
inputstr: ''
}
},
methods: {
add: function () {
// 子组件向上抛出一个方法
// 通知父组件, 接受一个方法:inputmsg
// 另外, 这个方法携带一个参数: this.inputstr
this.$emit("inputmsg", this.inputstr)
}
}
}
script>
<style scoped>
style>
<template>
<div>
<ul>
<li v-for=" (item, index) in list" :key="index">
{{item}}
li>
ul>
div>
template>
<script>
export default {
name: "Son2",
//Son2接收父组件传来的msg参数,msg参数是子组件抛出的inputstr字符串,而inputstr字符串是我们所输入的
props:['msg'],
data(){
return{
list: ["zs", "ls", "wu"]
}
},
//监听msg属性的变化,只要父组件的msg发生变化,就给列表添加值
watch: {
msg: function () {
this.list.push(this.msg)
}
}
}
script>
<style scoped>
style>
<template>
<div id="app">
<son1 class="son1" v-on:inputmsg="appinputmsg">son1>
<son2 class="son2" v-bind:msg="msg">son2>
div>
template>
<script>
import Son1 from "./components/Son1";
import Son2 from "./components/Son2";
export default {
name: 'App',
data() {
return{
msg: ''
}
},
methods: {
//接收到子组件Son1抛出的方法以后,触发这个方法,parame就是Son1抛出的参数
appinputmsg: function (parame) {
// alert("Son1抛出的" + parame)
//把Son1抛出的参数赋值给自己的msg属性,使用data的msg属性做一个中转存储
this.msg = parame
}
},
components: {
Son1,
Son2
}
}
script>
<style>
.son1{
width: 400px;
height: 400px;
border: 1px solid;
float: left;
}
.son2{
width: 500px;
height: 400px;
margin-left: 100px;
border: 1px solid;
float: left;
}
style>
可以用生活中的地铁车站来理解,从某个车站(子组件)到另一个车站(子组件),坐一条线路的地铁即可
import Vue from 'vue' // 导入Vue语法
const vue = new Vue() // 创建一个Vue对象
export default vue // 默认导出这个vue对象
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
// 从bus文件夹下的index.js 导过来一个Vue对象, 给这个对象起个名字叫vuebus
import vuebus from './bus/index.js' //由于index.js是默认代表首页写入底层语法中的,所以也可以不加
// 项目全局配置: $bus,可以是任何名称,但是习惯为$bus,也就是说,在项目里,所有Vue对象都有$bus这个属性
Vue.prototype.$bus = vuebus
//原有的vue配置语句
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
components: { App },
template: '<App/>'
})
<template>
<div>top3div>
template>
<script>
export default {
created() {
// TODO: 加载数据: 后端
}
}
script>
<style scoped>
style>
<template>
<div>top2div>
template>
<script>
export default {
created() {
// TODO: 加载数据: 后端
}
}
script>
<style scoped>
style>
<template>
<div>body2div>
template>
<script>
export default {
created() {
// TODO: 加载数据: 后端
}
}
script>
<style scoped>
style>
<template>
<div>
<body-left class="bodyleft">body-left>
<body-center class="bodycenter">body-center>
<body-right class="bodyright">body-right>
div>
template>
<script>
import BodyLeft from "./body/BodyLeft";
import BodyCenter from "./body/BodyCenter";
import BodyRight from "./body/BodyRight";
export default {
components: {
BodyLeft,
BodyRight,
BodyCenter
},
created() {
// TODO: 加载数据: 后端
}
}
script>
<style scoped>
.bodyleft{
width: 200px;
height: 500px;
background: #16c050;
float: left;
}
.bodycenter{
width: 700px;
height: 500px;
margin-left: 50px;
background: #c0b446;
float: left;
}
.bodyright{
width: 200px;
height: 500px;
margin-left: 50px;
background: #c0323c;
float: left;
}
style>
<template>
<div>
<input v-model="inputstr">
<button @click="add">添加button>
div>
template>
<script>
export default {
data(){
return{
inputstr: ''
}
},
methods: {
add: function () {
// 抛出方法addmsg,方法携带参数inputstr
// this: 值得是本对象
this.$bus.$emit("addmsg", this.inputstr)
}
}
}
script>
<style scoped>
style>
<template>
<div>
<ul>
<li v-for="(item, index) in list" :key="index">
{{item}}
li>
ul>
div>
template>
<script>
export default {
name: "BodyCenter",
data(){
return {
list: ["zs", "ls", "wu"]
}
},
//生命周期函数
created() {
// 监听一个bus事件: 这个事件的名称为addmsg
// $on: 监听一个事件
// res : 监听到的方法携带的参数
// =>: 箭头函数(ES6): 如下作用 把res当做一个参数传到匿名方法里,类似lambda表达式
this.$bus.$on("addmsg" , res => {
this.list.push(res)
})
}
}
script>
<style scoped>
style>
<template>
<div>BodyLeftdiv>
template>
<script>
export default {
name: "BodyLeft"
}
script>
<style scoped>
style>
<template>
<div>BodyRightdiv>
template>
<script>
export default {
name: "BodyRight"
}
script>
<style scoped>
style>
个人理解,如何看待VueBus?
- $bus这个对象/属性就可以看做一列地铁,因为它被配置为全局对象,每个vue文件都可以使用它,那么就相当于开辟了一个VueBus总线
- 在这列地铁上,某个vue文件抛出了某个方法并携带某个参数,相当于某位乘客上车了,直到另一个vue文件监听到了前面抛出的方法和参数,相当于这位乘客下车了。
import axios from 'axios';
//安装方法
npm install axios
//或
bower install axios
<script src="https://unpkg.com/axios/dist/axios.min.js">script>
//通过给定的ID来发送请求
axios.get('/user?ID=12345')
.then(function(response){
console.log(response);
})
.catch(function(err){
console.log(err);
});
//以上请求也可以通过这种方式来发送
axios.get('/user',{
params:{
ID:12345
}
})
.then(function(response){
console.log(response);
})
.catch(function(err){
console.log(err);
});
axios.post('/user',{
firstName:'Fred',
lastName:'Flintstone'
})
.then(function(res){
console.log(res);
})
.catch(function(err){
console.log(err);
});
<template>
<div id="app">
<img :src="imgurl">
div>
template>
<script>
export default {
data(){
return{
imgurl: ''
}
},
created() {
//请求后端数据
this.$axios.get("http://115.29.141.32:8084/api/mall/getGoodsByType?typeId=1")
//.then是专门用于处理返回参数的,res就是返回参数
.then(res => {
console.log(res)
console.log(res.data)
console.log(res.data.data)
this.imgurl = res.data.data[0].img
})
}
}
script>
<style>
style>