<template>
<h3>模板语法</h3>
<p>{{ msg }}</p>
</template>
<script>
export default{
data(){
return {
msg:"神奇的魔法"
}
}
}
</script>
<template>
<div v-bind:id="dynamicId" v-bind:class="dynamicClass">属性绑定</div>
</template>
<script>
export default{
data(){
return{
dynamicClass:"appClass",
dynamicId:"appId"
}
}
}
</script>
<style>
.appClass{
color:red;
font-size: 30px;
}
</style>
<template>
<div v-bind="objectOfAttrs">属性绑定</div>
</template>
<script>
export default{
data(){
return{
objectOfAttrs:{
class:"appclass",
id:"appid"
}
}
}
}
</script>
<style>
.appclass{
color:red;
font-size: 30px;
}
</style>
<template>
<h3>条件渲染</h3>
<div v-if="flag">你能看见我吗?</div>
<div v-else>那你还是看看我吧</div>
</template>
<script>
export default{
data(){
return{
flag:false
}
}
}
</script>
<template>
<h3>条件渲染</h3>
<div v-if="type==='A'">A</div>
<div v-else-if="type==='B'">B</div>
<div v-else-if="type==='C'">C</div>
<div v-else>Not A/B/C</div>
</template>
<script>
export default{
data(){
return{
type:'D'
}
}
}
</script>
<template>
<h3>列表渲染</h3>
<p v-for="item in names">{{ item }}</p>
</template>
<script>
export default{
data(){
return{
names:["百战程序员","尚学堂","IT"]
}
}
}
</script>
<template>
<h3>列表渲染</h3>
<div v-for="item in result">
<p>{{item.title}}</p>
<img v-bind:src="item.avatar" alt="">
</div>
</template>
<script>
export default {
data() {
return {
result: [
{
"id": 2261677,
"title": "鄂尔多斯|感受一座城市的璀璨夜景感受一座城市,除了白日里的车水马龙,喧嚣繁华",
"avatar": "https://pic.qyer.com/avatar/002/25/77/30/200?v=1560226451"
},
{
"id": 2261566,
"title": "成都这家洞穴暗黑风咖啡厅酷毙了!早C晚A走起\n成都天气这么热\n咖啡\n人必",
"avatar": "https://pic.qyer.com/avatar/011/07/08/69/200?v=1572185180"
},
{
"id": 2261662,
"title": "【川西新龙-措卡湖】措卡湖,意为“乱石丛中的黑色海水”,神秘小众原汁原味。深",
"avatar": "https://pic.qyer.com/avatar/009/88/48/58/200?v=1507386782"
}
]
}
}
}
</script>
<template>
<h3>内联事件处理器</h3>
<!-- <button v-on:click="count++">Add</button> -->
<button @click="count++">Add</button>
<p>{{count }}</p>
</template>
<script>
export default{
data(){
return{
count:0
}
}
}
</script>
<template>
<h3>方法事件处理器</h3>
<button @click="addCount">Add</button>
<p>{{count }}</p>
</template>
<script>
export default{
data(){
return{
count:0
}
},
//所有的方法函数都放在这里
methods:{
addCount(){
this.count++;
console.log("点击了")
}
}
}
</script>
<template>
<h3>Vue 中的 event 对象就是原生的 event</h3>
<button @click="addCount">Add</button>
<p>{{count }}</p>
</template>
<script>
export default{
data(){
return{
count:0
}
},
//所有的方法函数都放在这里
methods:{
addCount(event){
this.count++;
event.target.innerHTML="Add"+this.count;
}
}
}
</script>
<template>
<h3>事件传参</h3>
<p @click="getNameHandle(item, $event)" v-for="(item, index) of names" :key="index">{{ item }}</p>
</template>
<script>
export default {
data() {
return {
names: ["iwen", "ime", "frank"]
}
},
//所有的方法函数都放在这里
methods: {
getNameHandle(name, e) {
console.log(name, e);
}
}
}
</script>
<template>
<h3>事件修饰符</h3>
<a @click.prevent="clickHandle" href="https://itbaizhan.com">百战程序员</a>
</template>
<script>
export default{
data(){
return{
}
},
methods:{
clickHandle(e){
//阻止默认事件
// e.preventDefault();
console.log("点击了");
}
}
}
</script>
<template>
<h3>事件修饰符</h3>
<div @click="clickDiv">
<p @click.stop="clickP">测试冒泡</p>
</div>
</template>
<script>
export default{
data(){
return{
}
},
methods:{
clickDiv(){
console.log("div");
},
clickP(){
console.log("P");
}
}
}
</script>
<template>
<h3>数组变化侦听</h3>
<button @click="addListHandle"></button>
<ul>
<li v-for="(item,index) of names" :key="index">{{ item }}</li>
</ul>
<button @click="concatHandle">合并数组</button>
<h3>数组1</h3>
<p v-for="(item,index) of nums1" :key="index">{{ item }}</p>
<h3>数组 2</h3>
<p v-for="(item,index) of nums2" :key="index">{{ item }}</p>
</template>
<script>
export default{
data(){
return{
names:["iwen","ime","frank"],
nums1:[1,2,3,4,5],
nums2:[6,7,8,9,10]
}
},
methods:{
addListHandle(){
//会引起 UI 自动更新
// this.names.push("sarra")
//不会引起 UI 自动更新
//this.names.concat(["sarra"])
this.names = this.names.concat(["sarra"])
},
concatHandle(){
this.nums1 = this.nums1.concat(this.nums2)
}
}
}
</script>
<template>
<h3>{{ itbaizhan.name }}</h3>
<p>{{ itbaizhan.content.length>0?'Yes':'No' }}</p>
</template>
<script>
export default{
data(){
return{
itbaizhan:{
name:"百战程序员",
content:["前端","Java","Python"]
}
}
}
}
</script>
<template>
<h3>{{ itbaizhan.name }}</h3>
<p>{{ itbaizhanContent }}</p>
</template>
<script>
export default{
data(){
return{
itbaizhan:{
name:"百战程序员",
content:["前端","Java","Python"]
}
}
},
computed:{
itbaizhanContent(){
return this.itbaizhan.content.length>0?'Yes':'No'
}
}
}
</script>
<template>
<p :class="{ 'active':isActive,'text-danger':hasError}">Class 样式绑定</p>
</template>
<script>
export default{
data(){
return{
isActive:false
}
},
}
</script>
<style>
.active{
color: red;
font-size: 30px;
}
</style>
<template>
<p :class="{ 'active':isActive,'text-danger':hasError}">Class 样式绑定</p>
</template>
<script>
export default{
data(){
return{
isActive:true,
hasError:true
}
},
}
</script>
<style>
.active{
font-size: 30px;
}
.text-danger{
color: red;
}
</style>
<template>
<p :class="classObject">Class 样式绑定</p>
</template>
<script>
export default{
data(){
return{
classObject:{
'active':true,
'text-danger':false
}
}
},
}
</script>
<style>
.active{
font-size: 30px;
}
.text-danger{
color: red;
}
</style>
<template>
<p :class="[arrActive,arrHasError]">Class 样式绑定</p>
</template>
<script>
export default{
data(){
return{
arrActive:"active",
arrHasError:"text-danger"
}
},
}
</script>
<style>
.active{
font-size: 30px;
}
.text-danger{
color: red;
}
</style>
<template>
<div :style="{color:activeColor,fontSize:fontSize+'px'}">Style绑定</div>
</template>
<script>
export default{
data(){
return{
activeColor:'red',
fontSize:30
}
}
}
</script>
<template>
<div :style="styleObject">Style绑定</div>
</template>
<script>
export default{
data(){
return{
styleObject:{
color:'red',
fontSize:'30px'
}
}
}
}
</script>
<template>
<h3>侦听器</h3>
<p>{{ message }}</p>
<button @click="updateHandle">修改数据</button>
</template>
<script>
export default{
data(){
return{
message:"Hello"
}
},
methods:{
updateHandle(){
this.message = "World"
}
},
watch:{
message(newValue,oldValue){
console.log(newValue,oldValue)
}
}
}
</script>
<template>
<input type="text" v-model="message">
<p>{{ message }}</p>
</template>
<script>
export default{
data(){
return{
message:""
}
}
}
</script>
<template>
<input type="checkbox" id="checkbox" v-model="checked"/>
<label for="checkbox">{{ checked }}</label>
</template>
<script>
export default{
data(){
return{
message:"",
checked:false
}
}
}
</script>
<template>
<input type="text" v-model.lazy="message">
<p>{{ message }}</p>
</template>
<script>
export default{
data(){
return{
message:""
}
}
}
</script>
<template>
<div ref="container" class="container">{{ content }}</div>
<button @click="getElementHandle">获取元素</button>
<input type="text" ref="username">
</template>
<script>
export default{
data(){
return{
content:"内容"
}
},
methods:{
getElementHandle(){
this.$refs.container.innerHTML = "hahaha";
console.log(this.$refs.username.value)
}
}
}
</script>
<template>
<div>承载标签</div>
</template>
<script>
export default{}
</script>
<style scoped>
</style>
<template>
<div class="container">{{ message }}</div>
</template>
<script>
export default{
data(){
return{
message:"组件基础组成"
}
}
}
</script>
<style>
.container{
font-size:'30px';
color:red;
}
</style>
<template>
<!--3 标注组件 -->
<MyComponent/>
</template>
<script>
//1 引入组件
import MyComponent from "./components/MyComponent.vue"
export default{
//2 注入组件
components:{
MyComponent
}
}
</script>
<style>
</style>
<template>
<h3>Header</h3>
</template>
<style scoped>
h3{
width:100%;
height: 100px;
border: 5px solid #999;
text-align: center;
line-height: 100px;
box-sizing: border-box;
}
</style>
<template>
<div class="main">
<h3>Main</h3>
</div>
</template>
<script>
import Article from './Article.vue';
export default{
components:{
Article
}
}
</script>
<style scoped>
.main{
float: left;
width: 70%;
height: 400px;
border: 5px solid #999;
box-sizing: border-box;
border-top: 0px;
}
</style>
<template>
<div class="aside">
<h3>Aside</h3>
<Item/>
<Item/>
<Item/>
</div>
</template>
<script>
import Item from './Item.vue'
export default{
components:{
Item
}
}
</script>
<style scoped>
.aside{
float: right;
width:30%;
height: 600px;
border: 5px solid #999;
box-sizing: border-box;
border-left: 10;
border-top: 10;
}
</style>
<template>
<h3>Article</h3>
</template>
<style scoped>
h3{
width: 80%;
margin:0 auto;
text-align: center;
line-height: 100px;
box-sizing: border-box;
margin-top: 50px;
background: #999;
}
</style>
<template>
<h3>Item</h3>
</template>
<style scoped>
h3{
width:80%;
margin:0 auto;
text-align: center;
line-height: 100px;
box-sizing: border-box;
margin-top: 10px;
background: #999;
}
</style>
最外层注册全局都能用 Main.js 中
import { createApp } from 'vue'
import App from './App.vue'
import Header from './pages/Header.vue'
const app = createApp(App)
app.component("Header",Header)
app.mount("#app")=
如上节
<template>
<Parent/>
</template>
<script>
import Parent from './components/parent.vue'
export default{
components:{
Parent
}
}
</script>
<style>
</style>
<template>
<h3>Parent</h3>
<Child title="parent 数据" demo="测试"/>
</template>
<script>
import Child from './child.vue'
export default{
data(){
return{
}
},
components:{
Child
}
}
</script>
<template>
<h3>Child</h3>
<p>{{ title }}</p>
<p>{{ demo }}</p>
</template>
<script>
export default{
data(){
return{
}
},
props:["title","demo"]
}
</script>
<template>
<h3>Parent</h3>
<Child :title="message"/>
</template>
<script>
import Child from './child.vue'
export default{
data(){
return{
message:"Parent 数据!"
}
},
components:{
Child
}
}
</script>
<template>
<h3>Child</h3>
<p>{{ title }}</p>
</template>
<script>
export default{
data(){
return{
}
},
props:["title"]
}
</script>
<template>
<h3>Parent</h3>
<Child :title="message" :age="age" :names="names" :userInfo="userInfo"/>
</template>
<script>
import Child from './child.vue'
export default{
data(){
return{
message:"Parent 数据!",
age:10,
names:["iwen","jixiyu"],
userInfo:{
name:"iwen",
age:20
}
}
},
components:{
Child
}
}
</script>
<template>
<h3>Child</h3>
<p>{{ title }}</p>
<p>{{ age }}</p>
<ul>
<li v-for="(item,index) of names" :key="index">{{ item }}</li>
</ul>
<p>{{ userInfo.name }}</p>
<p>{{ userInfo.age }}</p>
</template>
<script>
export default{
data(){
return{
}
},
props:["title","age","names","userInfo"]
}
</script>
<template>
<h3>ComponentA</h3>
<ComponentB :title="title" :names="names"/>
</template>
<script>
import ComponentB from "./ComponentB.vue"
export default{
data(){
return{
title:"title",
names:["awin","jiangxiyu"]
}
},
components:{
ComponentB
}
}
</script>
<template>
<h3>Child</h3>
<button @click="clickEventHandle">传递数据</button>
</template>
<script>
export default{
data(){
return{
msg:"Child 数据!"
}
},
methods:{
clickEventHandle(){
this.$emit("someEvent",this.msg)
}
}
}
</script>
<template>
<h3>组件事件</h3>
<Child @someEvent="getHandle"></Child>
<p>父元素:{{ message }}</p>
</template>
<script>
import Child from './child.vue'
export default{
data(){
return {
message:""
}
},
components:{
Child
},
methods:{
getHandle(data){
this.message = data;
}
}
}
</script>
<template>
搜索:<input type="text" v-model="search">
</template>
<script>
export default{
data(){
return{
search:""
}
},
watch:{
search(newValue,oldValue){
this.$emit("searchEvent",newValue)
}
}
}
</script>
<template>
<h3>Main</h3>
<p>搜索内容为:{{ search }}</p>
<SearchCompoent @searchEvent="getSearch"/>
</template>
<script>
import SearchCompoent from './SearchCompoent.vue';
export default{
components:{
SearchCompoent
},
data(){
return{
search:""
}
},
methods:{
getSearch(data){
this.search = data;
}
}
}
</script>
<template>
<h3>ComponentA</h3>
<p>父元素:{{ message }}</p>
<ComponentB title="标题" :onEvent="dataFn"/>
</template>
<script>
import ComponentB from './ComponentB.vue'
export default{
data(){
return {
message:""
}
},
components:{
ComponentB,
},
methods:{
dataFn(data){
this.message = data
}
}
}
</script>
<template>
<h3>ComponentB</h3>
<p>{{ title }}</p>
<p>{{ onEvent('传递数据') }}</p>
</template>
<script>
export default{
data(){
return {
}
},
props:{
title:String,
onEvent:Function
}
}
</script>
<template>
<AttrComponents class="attr-containder"/>
</template>
<script>
import AttrComponents from './components/AttrComponents.vue'
export default{
components:{
AttrComponents
}
}
</script>
<style>
</style>
<template>
<!-- 透传属性必须唯一根元素 -->
<h3>透传属性</h3>
</template>
<script>
export default{
inheritAttrs:false
}
</script>
<style>
.attr-containder{
color:red
}
</style>
<template>
<SlotsBase>
<div>
<h3>插槽标题</h3>
<p>插槽内容</p>
</div>
</SlotsBase>
</template>
<script>
import SlotsBase from './components/SlotsBase.vue'
export default{
components:{
SlotsBase
}
}
</script>
<style>
</style>
<template>
<slot></slot>
<h3>插槽基础知识</h3>
</template>
<template>
<SlotsTow>
<template v-slot:header>
<h3>{{ message }}</h3>
</template>
<template v-slot:main>
<p>内容</p>
</template>
</SlotsTow>
</template>
<script>
import SlotsTow from './components/SlotsTow.vue'
export default{
components:{
SlotsTow
},
data(){
return{
message:"插槽内容续集"
}
}
}
</script>
<style>
</style>
<template>
<h3>Slots插槽续集</h3>
<slot name="header">插槽默认值</slot>
<hr>
<slot name="main">插槽默认值</slot>
</template>
<template>
<SlotsAttr v-slot="slotProps">
<h3>{{ currentTest }} - {{ slotProps.msg }}</h3>
</SlotsAttr>
</template>
<script>
import SlotsAttr from './components/SlotsAttr.vue'
export default{
data(){
return{
currentTest:"测试内容"
}
},
components:{
SlotsAttr
}
}
</script>
<style>
</style>
<template>
<h3>Slots 再续集</h3>
<slot :msg="childMessage"></slot>
</template>
<script>
export default{
data(){
return{
childMessage:"子组件数据"
}
}
}
</script>
<template>
<SlotsAttr>
<template #header="slotProps">
<h3>{{ currentTest }}-{{ slotProps.msg }}</h3>
</template>
<template #main="slotProps">
<p>{{ slotProps.job }}</p>
</template>
</SlotsAttr>
</template>
<script>
import SlotsAttr from './components/SlotsAttr.vue'
export default{
data(){
return{
currentTest:"测试内容"
}
},
components:{
SlotsAttr
}
}
</script>
<style>
</style>
<template>
<h3>Slots 再续集</h3>
<slot name="header" :msg="childMessage"></slot>
<slot name="main" :job="jobMessage"></slot>
</template>
<script>
export default{
data(){
return{
childMessage:"子组件数据",
jobMessage:"itbaizhan"
}
}
}
</script>
<template>
<h3>组件的声明周期</h3>
<p>{{ message }}</p>
<button @click="updateHandle"></button>
</template>
<script>
export default{
methods:{
updateHandle(){
this.message = "更新之后"
}
},
data(){
return{
message:"更新之前"
}
},
beforeCreate(){
console.log("组件创建之前")
},
created(){
console.log("组件创建之后")
},
beforeMount(){
console.log("组件渲染之前")
},
mounted(){
console.log("组件渲染之后")
},
beforeUpdate(){
console.log("组件更新之前")
},
updated(){
console.log("组件更新之后")
},
beforeUnmount(){
console.log("组件销毁之前")
},
unmounted(){
console.log("组件销毁之后")
}
}
</script>
<style>
</style>
<template>
<UserComponent/>
</template>
<script>
import UserComponent from './components/UserComponent.vue'
export default{
components:{
UserComponent
}
}
</script>
<style>
</style>
<template>
<h3>组件声明周期函数应用</h3>
<p ref="name">百战程序员</p>
</template>
<script>
export default{
beforeMount(){
console.log(this.$refs.name);
},
mounted(){
console.log(this.$refs.name)
}
}
</script>
<style>
</style>
<template>
<h3>组件声明周期函数应用</h3>
<p ref="name">百战程序员</p>
<ul>
<li v-for="(item, index) of banner" :key ="index">
<h3>{{ item.title }}</h3>
<p>{{ item.content }}</p>
</li>
</ul>
</template>
<script>
export default {
data() {
return {
banner: []
}
},
mounted() {
//网络请求
this.banner = [
{
"title": "我在爱尔兰",
"content": "爱尔兰(爱尔兰语: Poblacht na hEireann;英语: Republic of Ireland),是一个..."
},
{
"title": "一个人的东京",
"content": "东京(Tokyo)是日本国的首都,是亚洲第一大城市,世界第二大城市。全球最的经济中心..."
},
{
"title": "普罗旺斯的梦",
"content": "普罗旺斯(Provence)位于法国东南部,毗邻地中海和意大利,从地中海沿岸延伸到内陆..."
}
]
},
}
</script>
<style></style>
<template>
<ComponentA/>
<ComponentB/>
<component :is="tabComponent"></component>
<button @click="changeHandle">切换组件</button>
</template>
<script>
import ComponentA from './components/ComponentA.vue'
import ComponentB from './components/ComponentB.vue'
export default{
data(){
return{
tabComponent: "ComponentA"
}
},
components:{
ComponentA,
ComponentB
},
methods:{
changeHandle(){
this.tabComponent = this.tabComponent == "ComponentA"?"ComponentB":"ComponentA"
}
}
}
</script>
<style>
</style>
<template>
<KeepAlive>
<component :is="tabComponent"></component>
</KeepAlive>
<button @click="changeHandle">切换组件</button>
</template>
<script>
import ComponentA from './components/ComponentA.vue'
import ComponentB from './components/ComponentB.vue'
export default{
data(){
return{
tabComponent: "ComponentA"
}
},
components:{
ComponentA,
ComponentB
},
methods:{
changeHandle(){
this.tabComponent = this.tabComponent == "ComponentA"?"ComponentB":"ComponentA"
}
}
}
</script>
<style>
</style>
<template>
<h3>ComponentA</h3>
<p>{{ message }}</p>
<button @click="updateHandle">更新数据</button>
</template>
<script>
export default{
beforeUnmount(){
console.log("组件卸载前")
},
unmounted(){
console.log("组件卸载后")
},
data(){
return{
message:"老数据"
}
},
methods:{
updateHandle(){
this.message = "新数据"
}
}
}
</script>
<template>
<h3>ComponentB</h3>
</template>
<template>
<KeepAlive>
<component :is="tabComponent"></component>
</KeepAlive>
<button @click="changeHandle">切换组件</button>
</template>
<script>
import { defineAsyncComponent } from 'vue'
import ComponentA from './components/ComponentA.vue'
// import ComponentB from './components/ComponentB.vue'
const ComponentB = defineAsyncComponent(()=>
import("./components/ComponentB.vue")
)
export default{
data(){
return{
tabComponent: "ComponentA"
}
},
components:{
ComponentA,
ComponentB
},
methods:{
changeHandle(){
this.tabComponent = this.tabComponent == "ComponentA"?"ComponentB":"ComponentA"
}
}
}
</script>
<style>
</style>
<template>
<h3>祖宗</h3>
<Father />
</template>
<script>
import Father from './components/Father.vue'
export default{
// provide:{
// message:"爷爷的财产"
// },
data(){
return{
message:"爷爷的财产"
}
},
provide(){
return{
message:this.message
}
},
components:{
Father
}
}
</script>
<style>
</style>
<template>
<h3>父亲</h3>
<Son />
</template>
<script>
import Son from './Son.vue'
export default{
props:{
title:{
type:String
}
},
components:{
Son
}
}
</script>
<template>
<h3>孩子</h3>
<p>{{ fullMessage }}</p>
</template>
<script>
export default{
props:{
title:{
type:String
}
},
inject:["message"],
data(){
return{
fullMessage:this.message
}
}
}
</script>
import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
app.provide("golabData","我是全局数据")
app.mount("#app")
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="icon" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vite App</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
app.mount("#app")
<template>
<router-link to="/home">首页</router-link>
<router-link to="/blog">博客</router-link>
<router-view></router-view>
</template>
<script>
export default{
name:'App'
}
</script>
<style>
</style>
import {createRouter,createWebHashHistory} from "vue-router"
import Home from "./components/Home.vue"
import Blog from "./components/Blog.vue"
const router = createRouter({
history: createWebHashHistory(),
routes:[
{
path:"/home",
component:Home
},
{
path:"/blog",
component:Blog
}
]
})
export default router;
import { createApp } from 'vue'
import App from './App.vue'
import router from './router.js'
const app = createApp(App)
app.use(router);
app.mount("#app")