v-mode指令 大多用在表单上进行双向绑定
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<div id="app">
<input type="text" v-model="message">
{{message}}
div>
<script src="../../lib/vue.js">script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊'
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<div id="app">
<h1>v-model: 双向绑定h1>
<input type="text" v-model="message"><br>
<h1>v-bind: 单向绑定,data数据传递到页面。 input事件: 监听用户输入的数据传递到data中h1>
<input type="text" :value="message" @input="valueChange"><br>
<input type="text" :value="message" @input="message = $event.target.value">
<h2>{{message}}h2>
div>
<script src="../../lib/vue.js">script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊'
},
methods: {
valueChange(event) {
// event 不需要传参,会自动传值
console.log(event)
// event.target.value 表单中的输入值
this.message = event.target.value;
}
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<div id="app">
<label for="male">
<input type="radio" id="male" value="男" v-model="sex">男
label>
<label for="female">
<input type="radio" id="female" value="女" v-model="sex">女
label>
<h2>您选择的性别是: {{sex}}h2>
div>
<script src="../../lib/vue.js">script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
sex: '女'
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<div id="app">
<h1>1.checkbox单选框: 对应 Boolean类型h1>
<label for="agree">
<input type="checkbox" id="agree" v-model="isAgree">同意协议
label>
<h5>您选择的是: {{isAgree}}h5>
<button :disabled="!isAgree">下一步button>
<h1>2.checkbox多选框: 对应 数组类型h1>
<input type="checkbox" value="篮球" v-model="hobbies">篮球
<input type="checkbox" value="足球" v-model="hobbies">足球
<input type="checkbox" value="乒乓球" v-model="hobbies">乒乓球
<input type="checkbox" value="羽毛球" v-model="hobbies">羽毛球
<h5>您的爱好是: {{hobbies}}h5>
<h5>值绑定: 从data 中获取数据进行 展示h5>
<label v-for="item in originHobbies" :for="item">
<input type="checkbox" :value="item" :id="item" v-model="hobbies">{{item}}
label>
div>
<script src="../../lib/vue.js">script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
isAgree: false, // 单选框
hobbies: [], // 多选框,
originHobbies: ['篮球', '足球', '乒乓球', '羽毛球', '台球', '高尔夫球']
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<div id="app">
<h1>1.选择一个:字符串类型h1>
<select name="abc" v-model="fruit">
<option value="苹果">苹果option>
<option value="香蕉">香蕉option>
<option value="榴莲">榴莲option>
<option value="葡萄">葡萄option>
select>
<h4>您选择的水果是: {{fruit}}h4>
<h1>2.选择多个:数组类型h1>
<select name="abc" v-model="fruits" multiple size="3">
<option value="苹果">苹果option>
<option value="香蕉">香蕉option>
<option value="榴莲">榴莲option>
<option value="葡萄">葡萄option>
<option value="橘子">橘子option>
select>
<h4>您选择的水果是: {{fruits}}h4>
div>
<script src="../../lib/vue.js">script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
fruit: '香蕉',
fruits: []
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<div id="app">
<h1>1.修饰符: lazy: 点击回车的时候 会进行 实时绑定h1>
<input type="text" v-model.lazy="message">
<h2>{{message}}h2>
<h1>2.修饰符: number: v-model默认绑定为 string类型, number改变类型h1>
<input type="number" v-model.number="age">
<h2>{{age}}-{{typeof age}}h2>
<h1>3.修饰符: trim :去掉表单中输入的两边的空格h1>
<input type="text" v-model.trim="name">
<h2>您输入的名字:{{name}}h2>
div>
<script src="../../lib/vue.js">script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
age: 0,
name: ''
}
})
var age = 0
age = '1111'
age = '222'
script>
body>
html>
Vue.extend({})
Vue.component('my-cpn', cpnC)
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>01-组件化的基本使用title>
head>
<body>
<div id="app">
<my-cpn>my-cpn>
<my-cpn>my-cpn>
<div>
<div>
<my-cpn>my-cpn>
div>
div>
div>
<my-cpn>my-cpn>
<script src="../../lib/vue.js">script>
<script>
// ES6语法:定义字符串的 单引号、双引号 和 `` 的区别
const a = '12' +
'34';
const b = `we
qw`;
// 1.创建组件构造器对象
const cpnC = Vue.extend({
template: `
我是标题
我是内容, 哈哈哈哈
我是内容, 呵呵呵呵
`
})
// 2.注册组件
Vue.component('my-cpn', cpnC)
const app = new Vue({
el: '#app',
data: {
message: '你好啊'
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>02-全局组件和局部组件title>
head>
<body>
<div id="app">
<cpna>cpna>
<cpnb>cpnb>
div>
<hr>
<h1>下面的不显示,说明 局部组件 只能使用在挂载的地方h1>
<div id="app2">
<cpna>cpna>
div>
<script src="../../lib/vue.js">script>
<script>
// 1.创建局部组件构造器
const cpna = Vue.extend({
template: `
我是标题a: 局部组件
我是内容a,哈哈哈哈啊
`
})
// 2. 全局组件
const cpnb = Vue.extend({
template: `
我是标题b: 全局组件
我是内容,哈哈哈哈啊
`
})
// 3.注册组件(全局组件, 意味着可以在多个Vue的实例下面使用)
Vue.component('cpnb', cpnb)
// 疑问: 怎么注册的组件才是局部组件了?
const app = new Vue({
el: '#app',
data: {
message: '你好啊'
},
// 局部组件定义的位置
components: {
// cpnA 使用组件时的标签名
cpna: cpna
}
})
const app2 = new Vue({
el: '#app2'
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>03-父组件和子组件title>
head>
<body>
<div id="app">
<cpn2>cpn2>
div>
<script src="../../lib/vue.js">script>
<script>
// 1.创建第一个组件构造器(子组件)
const cpnC1 = Vue.extend({
template: `
我是标题1: 子组件
我是内容, 哈哈哈哈
`
})
// 2.创建第二个组件构造器(父组件)
const cpnC2 = Vue.extend({
template: `
我是标题2: 父组件
我是内容, 呵呵呵呵
`,
components: {
cpn1: cpnC1
}
})
// root组件
const app = new Vue({
el: '#app',
data: {
message: '你好啊'
},
components: {
cpn2: cpnC2
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>04-组件的语法糖注册方式title>
head>
<body>
<div id="app">
<cpn1>cpn1>
<cpn2>cpn2>
div>
<script src="../../lib/vue.js">script>
<script>
// 1.全局组件注册的语法糖
// 1.创建组件构造器
// const cpn1 = Vue.extend({
// template: `
//
// 我是标题1
// 我是内容, 哈哈哈哈
//
// `
// })
// 2.注册全局组件
Vue.component('cpn1', {
template: `
我是标题1: 全局组件
我是内容, 哈哈哈哈
`
})
// 2.注册局部组件的语法糖
const app = new Vue({
el: '#app',
data: {
message: '你好啊'
},
components: {
'cpn2': {
template: `
我是标题2: 局部组件
我是内容, 呵呵呵
`
}
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>05-组件模板的分离写法title>
head>
<body>
<div id="app">
<cpn1>cpn1>
<cpn2>cpn2>
div>
<script type="text/x-template" id="cpn1">
<div>
<h2>我是标题: script标签 创建的模版-全局组件</h2>
<p>我是内容,哈哈哈</p>
</div>
script>
<template id="cpn2">
<div>
<h2>我是标题: template标签 创建的模版-全局组件h2>
<p>我是内容,呵呵呵p>
div>
template>
<script src="../../lib/vue.js">script>
<script>
// 2.注册一个全局组件
Vue.component('cpn1', {
template: '#cpn1'
})
Vue.component('cpn2', {
template: '#cpn2'
})
const app = new Vue({
el: '#app',
data: {
message: '你好啊'
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>06-组件中的数据存放问题title>
head>
<body>
<div id="app">
<cpn>cpn>
<cpn>cpn>
div>
<template id="cpn">
<div>
<h2>{{title}}h2>
<p>我是全局组件 模版中的数据,呵呵呵p>
div>
template>
<script src="../../lib/vue.js">script>
<script>
// 1.注册一个全局组件
Vue.component('cpn', {
template: '#cpn',
data() {
return {
title: 'abc'
}
}
})
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
// title: '我是标题'
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>07-组件中的data为什么是函数title>
head>
<body>
<div id="app">
<cpn>cpn>
div>
<template id="cpn">
<div>
<h2>全局组件-模版中-当前计数: {{counter}}h2>
<button @click="increment">+button>
<button @click="decrement">-button>
div>
template>
<script src="../../lib/vue.js">script>
<script>
// 1.注册组件
const obj = {
counter: 0
}
/*
* 为什么data在组件中必须是一个函数呢?
* 1. 首先,如果不是一个函数,Vue直接就会报错。
* 2. 其次,原因是在于Vue让每个组件对象都返回一个新的对象,因为如果是同一个对象的,组件在多次使用后会相互影响。
* */
Vue.component('cpn', {
template: '#cpn',
// data() {
// return {
// counter: 0
// }
// },
data() {
return obj
},
methods: {
increment() {
this.counter++
},
decrement() {
this.counter--
}
}
})
const app = new Vue({
el: '#app',
data: {
message: '你好啊'
}
})
script>
<script>
// const obj = {
// name: 'why',
// age: 18
// }
//
// function abc() {
// return obj
// }
//
// let obj1 = abc()
// let obj2 = abc()
// let obj3 = abc()
//
// obj1.name = 'kobe'
// console.log(obj2);
// console.log(obj3);
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>08-组件通信-父组件向子组件传递数据title>
head>
<body>
<div id="app">
<h1>正确写法, 使用第一种 props写法: 使用 v-bind 进行传值h1>
<cpn v-bind:cmessage="message" :cmovies="movies">cpn>
<h1>如果不加 v-bind, 下面这个会将 message、movies 作为 字符串传递给 cmessage、movies 属性h1>
<cpn cmessage="message" cmovies="movies" >cpn>
<h1>正确写法, 使用第二种 props写法, cmessage 没传值, 使用的是默认值h1>
<cpn v-bind:cmovies="movies">cpn>
div>
<template id="cpn">
<div>
<ul>
<li v-for="item in cmovies">{{item}}li>
ul>
<h2>{{cmessage}}h2>
div>
template>
<script src="../../lib/vue.js">script>
<script>
// 父传子: props: properties
const cpn = {
template: '#cpn',
// 第一种写法
// props: ['cmovies', 'cmessage'],
// 第二种写法
props: {
// 1.类型限制
// cmovies: Array,
// cmessage: String,
// 2.提供一些默认值, 以及必传值
cmessage: {
type: String,
default: 'aaaaaaaa',
required: true // 为true时,必须要传,不传则报错
},
// 类型是对象或者数组时, 默认值必须是一个函数
cmovies: {
type: Array,
default() {
return []
}
}
},
data() {
return {}
},
methods: {
}
}
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
movies: ['海王', '海贼王', '海尔兄弟']
},
components: {
cpn
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>09-组件通信-父传子(props中的驼峰标识)title>
head>
<body>
<div id="app">
<cpn :c-info="info" :child-my-message="message" >cpn>
div>
<template id="cpn">
<div>
<h2>{{cInfo}}h2>
<h2>{{childMyMessage}}h2>
div>
template>
<script src="../../lib/vue.js">script>
<script>
const cpn = {
template: '#cpn',
props: {
cInfo: {
type: Object,
default() {
return {}
}
},
childMyMessage: {
type: String,
default: ''
}
}
}
const app = new Vue({
el: '#app',
data: {
info: {
name: 'why',
age: 18,
height: 1.88
},
message: 'aaaaaa'
},
components: {
cpn
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>10-组件通信-子传父(自定义事件)title>
head>
<body>
<div id="app">
<cpn @item-click="cpnClick">cpn>
div>
<template id="cpn">
<div>
<button v-for="item in categories"
@click="btnClick(item)">
{{item.name}}
button>
div>
template>
<script src="../../lib/vue.js">script>
<script>
/*
* 1.子组件
* */
const cpn = {
template: '#cpn',
data() {
return {
categories: [
{id: 'aaa', name: '热门推荐'},
{id: 'bbb', name: '手机数码'},
{id: 'ccc', name: '家用家电'},
{id: 'ddd', name: '电脑办公'},
]
}
},
methods: {
btnClick(item) {
console.log(item);
// 发射事件: 自定义事件, item:为参数, item-click:事件名称
this.$emit('item-click', item)
}
}
}
/*
* 2.父组件
* */
const app = new Vue({
el: '#app',
data: {
message: '你好啊'
},
components: {
cpn
},
methods: {
cpnClick(item) {
console.log('cpnClick', item);
}
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>12-组件通信-父子组件通信案例(watch实现)title>
head>
<body>
<div id="app">
<cpn :number1="num1"
:number2="num2"
@num1change="num1change"
@num2change="num2change"/>
div>
<template id="cpn">
<div>
<h2>props:{{number1}}h2>
<h2>data:{{dnumber1}}h2>
<input type="text" v-model="dnumber1">
<h2>props:{{number2}}h2>
<h2>data:{{dnumber2}}h2>
<input type="text" v-model="dnumber2">
div>
template>
<script src="../../lib/vue.js">script>
<script>
const app = new Vue({
el: '#app',
data: {
num1: 1,
num2: 0
},
methods: {
num1change(value) {
this.num1 = parseFloat(value)
},
num2change(value) {
this.num2 = parseFloat(value)
}
},
components: {
cpn: {
template: '#cpn',
props: {
number1: Number,
number2: Number,
name: ''
},
data() {
return {
dnumber1: this.number1,
dnumber2: this.number2
}
},
watch: {
dnumber1(newValue) {
this.dnumber2 = newValue * 100;
this.$emit('num1change', newValue);
},
dnumber2(newValue) {
this.number1 = newValue / 100;
this.$emit('num2change', newValue);
}
}
}
}
})
script>
body>
html>
13-组件访问-父访问子-$children-$refs.html
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>13-组件访问-父访问子-children-refstitle>
head>
<body>
<div id="app">
<cpn>cpn>
<cpn ref="aaa">cpn>
<button @click="btnClink">按钮button>
div>
<template id="cpn">
<div>
<span>我是一个演员span>
div>
template>
<script src="../../lib/vue.js">script>
<script>
const app = new Vue({
el: '#app',
data: {},
methods: {
btnClink() {
// 父组件 访问 子组件
/*
* 1. $children
* */
console.log('this:', this)
console.log('this.$children:', this.$children)
console.log('-------------------')
this.$children[0].showMessage();
for (let c of this.$children) {
console.log('c.name: ', c.name)
c.showMessage()
}
this.$children[1].name; // 这样可以指定某个组件对象,但是不固定,所以这样不好
/*
* 2. $ref => 对象类型,默认是一个空的对象 ref='bbb
* */
console.log(this.$refs.aaa.name)
}
},
components: {
cpn: {
template: '#cpn',
props: {},
data() {
return {
name: '我是子组件的 name'
}
},
methods: {
showMessage() {
console.log('子组件方法-showMessage');
}
}
}
}
})
script>
body>
html>
14-组件访问-子访问父-$parent-$root.html
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>14-组件访问-子访问父-parent-roottitle>
head>
<body>
<div id="app">
<cpn>cpn>
div>
<template id="cpn">
<div>
<h2>我是cpn组件h2>
<ccpn>ccpn>
div>
template>
<template id="ccpn">
<div>
<h2>我是ccpn组件h2>
<button @click="btnClick">按钮button>
div>
template>
<script src="../../lib/vue.js">script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊'
},
components: {
cpn: {
template: '#cpn',
data(){
return {
name: '我是ccpn组件的 name'
}
},
methods: {
btnClink(){
console.log('btnClink');
}
},
components: {
ccpn: {
template: '#ccpn',
methods: {
btnClick(){
// 1. 访问父组件 $parent
console.log('this.$parent: ', this.$parent);
console.log('this.$parent.name: ', this.$parent.name);
console.log('------------------------');
// 2. 访问根组件$root
console.log('this: ', this);
console.log('this.$root: ', this.$root);
console.log('this.$root.message: ', this.$root.message);
}
}
}
}
}
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>01-slot-插槽的基本使用title>
head>
<body>
<div id="app">
<cpn>cpn>
<hr>
<cpn><span>我是传过来的:哈哈哈span>cpn>
<hr>
<cpn><i>我是传过来的:呵呵呵i>cpn>
<hr>
<cpn>
<i>我是传过来的:呵呵呵i>
<div>div元素div>
<p>p元素p>
cpn>
<hr>
div>
<template id="cpn">
<div>
<h2>我是组件h2>
<p>我是组件, 哈哈哈p>
<slot><button>按钮button>slot>
div>
template>
<script src="../../lib/vue.js">script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊'
},
components: {
cpn: {
template: '#cpn'
}
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>02-slot-具名插槽的使用title>
head>
<body>
<div id="app">
<cpn><span slot="center">centerspan>cpn>
<hr>
<cpn><button slot="left">leftbutton>cpn>
div>
<template id="cpn">
<div>
<slot name="left"><span>左边span>slot>
<slot name="center"><span>中间span>slot>
<slot name="right"><span>右边span>slot>
div>
template>
<script src="../../lib/vue.js">script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊'
},
components: {
cpn: {
template: '#cpn'
}
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>03-什么是编译的作用域title>
head>
<body>
<div id="app">
<cpn v-show="isShow">cpn>
<hr>
<cpn v-for="item in names">cpn>
div>
<template id="cpn">
<div>
<h2>我是子组件h2>
<p>我是内容, 哈哈哈p>
<button v-show="isShow">按钮button>
div>
template>
<script src="../../lib/vue.js">script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
isShow: true
},
components: {
cpn: {
template: '#cpn',
data() {
return {
isShow: false,
names: [1, 2, 3, 4]
}
}
},
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>04-作用域插槽的案例title>
head>
<body>
<div id="app">
<cpn>cpn>
<cpn>
<template slot-scope="slot">
{item}}-->
<span>{{slot.data.join(' - ')}}span>
template>
cpn>
<cpn>
<template slot-scope="slot">
{item}} * -->
<span>{{slot.data.join(' * ')}}span>
template>
cpn>
div>
<template id="cpn">
<div>
<slot :data="pLanguages">
<ul>
<li v-for="item in pLanguages">{{item}}li>
ul>
slot>
div>
template>
<script src="../../lib/vue.js">script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊'
},
components: {
cpn: {
template: '#cpn',
data() {
return {
pLanguages: ['JavaScript', 'C++', 'Java', 'C#', 'Python', 'Go', 'Swift']
}
}
}
}
})
script>
body>
html>