本周做项目用到了 vue,周末复习整理一下~
全局变量 Vue、函数 createApp、参数传入对象
在模板 template 里面编写 html
data:funcition(){return{titile:“hahh”}} 这个函数会返回一个新的对象
使用 const app 接收
这个对象使用挂载方法:参数为挂载到哪里:使用 #+id
底层是 querySelector
vue:将 template 里面的内容挂载到对应元素里面
本地引入 ctrl+a:全选 下载复制代码到 lib/vue.js
<div id="app">div>
<script src="./lib/vue.js">script>
<script>
const app = Vue.createApp({
template: `{{title}}
{{message}}
`,
data: function () {
return {
title: "hello world",
message: "你好呀这是美好的今天!"
}
}
})
app.mount("#app")
script>
<body>
<div id="app">div>
<script src="./lib/vue.js">script>
<script>
const app = Vue.createApp({
template: `
{{message}}
电影列表
- {{item}}
`,
data: function () {
return {
message: "是一个优秀的孩子",
movies: ["aaaaaa", "bbbbbbb", "gggggggggg", "ttttttttttt", "ooooooooo"]
}
}
})
app.mount("#app")
script>
body>
<body>
<div id="app">div>
<script src="./lib/vue.js">script>
<script>
const app = Vue.createApp({
template: `
当前计数:{{counter}}
`,
data: function () {
return {
counter: 0
}
},
methods: {
increment() {
console.log("+1");
this.counter++
},
decrement() {
console.log("-1");
this.counter--
}
}
})
app.mount("#app")
script>
body>
声明式关注 what to do:剩下的交给框架
命令式
<body>
<h2>当前计数:<span class="counter">span>h2>
<button class="add">+1button>
<button class="sub">-1button>
<script>
// 1. 获取dom
const h2El = document.querySelector("h2")
const counterEl = document.querySelector(".counter")
const addBtnEl = document.querySelector(".add")
const subBtnEl = document.querySelector(".sub")
// 2. 定义一个变量记录数据
let counter = 100
counterEl.textContent = counter
// 3. 监听按钮的点击
addBtnEl.onclick = function () {
counter++
counterEl.textContent = counter
}
subBtnEl.onclick = function () {
counter--
counterEl.textContent = counter
}
script>
body>
<div id="lili">{{message}}
<button @click="changeText">改变文字button>
div>
<script src="./lib/vue.js">script>
<script>
const app=Vue.createApp({
data:function(){
return{
message:"hello lili"
}
},
methods:{
changeText:function(){
this.message="你好"
console.log("hahh");
}
}
})
app.mount("#lili")
script>
App.vue
<template></template>
<script></script>
react开发:
vue 开发
也支持 jsx 开发
基于 html 的模板语法
<h2 v-bind v-for v-once>dafsf{{}}</h2>
在 template 中允许开发者以声明式的方式将 dom 和底层组件实例的数据绑定在一起
底层中将模板编译成虚拟 dom 渲染函数
希望把数据显示到模板(template)中,使用最多的语法是 “Mustache”语法 (双大括号) 的文本插值
差值语法、大胡子语法
基本使用 {{counter}}
表达式:{{counter*2}}
三元运算符
在里面不能定义语句
调用 methods 里面的函数
<body>
<div id="lili">
<h1> {{message}}h1>
<h2>当前计数:{{counter}}h2>
<h2>双倍计数:{{counter*2}}h2>
<h2>展示的信息:{{infos.split(" ")}}h2>
<h1>{{age>=18?"成年人":"小学生"}}h1>
<h4>{{formatTime("yingyu")}}h4>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data: function () {
return {
message: "hello lili",
counter: 0,
infos: "my name is lili",
age: 99
}
},
methods:{
formatTime(date){
return "2023-5-16"+date
}
}
})
app.mount("#lili")
script>
body>
<h2 v-text="message">h2>
html
标签等等 <h1 v-html="content">h1>
<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>Documenttitle>
<style>
[v-clock] {
display: none;
}
style>
head>
<body>
<div id="lili">
<h1 v-cloak>{{message}}h1>
div>
<script src="../lib/vue.js">script>
<script>
setTimeout(() => {
const app = Vue.createApp({
data: function () {
return {
message: "hello lili"
}
}
})
app.mount("lili")
}, 3000)
script>
body>
<body>
<div id="lili" v-memo="[name]">
<p>{{message}}p>
<h2>姓名:{{name}}h2>
<h2>年龄:{{age}}h2>
<h2>身高:{{height}}h2>
<button @click="changeInfo">改变信息button>
div>
<script src="../lib/vue.js">script>
<script>
const app=Vue.createApp({
data:function(){
return{
message:"hello lili",
name:"lili",
age:99,
height:1000
}
},
methods:{
changeInfo(){
this.name = "kobe"
}
}
})
app.mount("#lili")
script>
body>
<body>
<div id="lili">
<button @click="switchImage">button>
<img v-bind:src=" showImgURL" alt="">
<a v-bind:href="href">淘宝a>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data: function () {
return {
message: "hello lili",
imgURL1: "http://baidu.com",
imgURL2: "http://baiduuu.com",
showImgURL: "http://baiduuu.com",
href: "http://taobao.com"
}
},
methods: {
switchImage: function () {
this.showImgURL = this.showImgURL === this.imgURL1 ? this.imgURL2 : this.imgURL1
}
}
})
app.mount("#lili")
script>
body>
:class="[]"
:class="{css名字:Boolean}"
<body>
<div id="lili">
<h2 :class="classes">Hello lilih2>
<button :class="isActive? 'active' : ''" @click="btnClick">我是按钮yiyibutton>
<button :class="{active:isActive}" @click="btnClick">我是按钮ererbutton>
<button :class="{active:isActive,lili:false,jiamiao:true}" @click="btnClick">我是按钮sansanbutton>
<button class="btn" :class="{active:isActive}">按钮sisibutton>
<button class="btn" :class="getDynamicClasses()" @click="btnClick">按钮wuwu
button>
<h1 :class="['abc','cba','kkk']">我是一个大标题h1>
<h2 :class="['bbb',className]">我是一个中标题h2>
<h3 :class="['ccc',{'active':isActive}]">我是一个小标题h3>
<h3>h3>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data: function () {
return {
message: "hello lili",
classes: "abc cba",
isActive: false,
className: 'nihao'
}
},
methods: {
btnClick() {
this.isActive = !this.isActive
},
getDynamicClasses() {
return { active: this.isActive }
}
}
})
app.mount("#lili")
script>
body>
style=“{}”
<body>
<div id="lili">
<h1 style="color:red">哈哈哈哈h1>
<h1 :style="{color:fontColor,fontSize:fontsize}">哈哈哈哈h1>
<h2 :style="styleObj">haahhhhhhh2>
<h1 :style="[styleObj,{backgroundColor:'blue'}]">你好呀h1>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data: function () {
return {
message: "hello lili",
fontColor: "blue",
fontsize: "50px",
styleObj: {
color: "green"
}
}
}
})
app.mount("#lili")
script>
body>
<body>
<div id="lili">
<h2 :[title]="'aaaa'">蛋白爱人h2>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data: function () {
return {
message: "hello lili",
title: "word"
}
}
})
app.mount("#lili")
script>
body>
希望绑定多个属性,v-bind=某一个对象
<h2 v-bind="infos"></h2>
infos:{hah:88},info 对象会被拆解成 div 的各个属性
经常用于给组件传递参数
<body>
<div id="lili">{{message}}
<h1 :name="name" :age="age" :height="height">hello lilih1>
<h1 v-bind="props">propsh1>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data: function () {
return {
message: "hello lili",
props: {
name: 'lili',
age: 12,
height: 1.99
},
name: 'lili',
age: 12,
height: 1.99
}
}
})
app.mount("#lili")
script>
body>
<style>
.box {
width: 100px;
height: 100px;
background-color: orange;
margin-top: 10px;
}
style>
head>
<body>
<div id="app">
<div class="box" v-on:click="divClick">div>
<div class="box" @click="divClick">div>
<h2>{{ counter }}h2>
<button @click="increment">+1button>
<button @click="counter++">+1button>
<div class="box" @mousemove="divMousemove">div>
<div class="box" @click="divClick" @mousemove="divMousemove">div>
div>
<script src="../lib/vue.js">script>
<script>
// 1.创建app
const app = Vue.createApp({
// data: option api
data: function() {
return {
counter: 0
}
},
methods: {
divClick() {
console.log("divClick")
},
increment() {
this.counter++
},
divMousemove() {
console.log("divMousemove")
}
}
})
// 2.挂载app
app.mount("#app")
script>
情况一:如果该方法不需要额外参数,那么方法后的()可以不添加
情况二:如果需要同时传入某个参数,同时需要 event 时,可以通过 $event 传入事件
默认参数:event 对象
绑定对象的时候没有任何的参数那么 event 对象会被默认传递进来
不能理解成函数调用,vue 会在发生事件的时候传递参数
<div id="app">
<button @click="btn1Click">按钮1button>
<button @click="btn2Click('wtthy', age)">按钮2button>
<button @click="btn3Click('wtthy', age, $event)">按钮3button>
div>
<script src="../lib/vue.js">script>
<script>
// 1.创建app
const app = Vue.createApp({
// data: option api
data: function() {
return {
message: "Hello Vue",
age: 18
}
},
methods: {
// 1.默认参数: event对象
// 总结: 如果在绑定事件的时候, 没有传递任何的参数, 那么event对象会被默认传递进来
btn1Click(event) {
console.log("btn1Click:", event)
},
// 2.明确参数:
btn2Click(name, age) {
console.log("btn2Click:", name, age)
},
// 3.明确参数+event对象
btn3Click(name, age, event) {
console.log("btn3Click:", name, age, event)
}
}
})
// 2.挂载app
app.mount("#app")
script>
<body>
<div id="lili">
<div class="box" @click="divClick">
<button @click.stop="btnClick">buttonbutton>
div>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data: function () {
return {
message: "hello lili"
}
},
methods: {
divClick() {
console.log("divClick");
},
btnClick() {
console.log("btnClick");
}
}
})
app.mount("#lili")
script>
body>
<body>
<div id="lili">
<div class="info" v-if="Object.keys(info).length ">
<h2>个人信息h2>
<ul>
<li>姓名:{{info.name}}li>
<li>年龄:{{info.age}}li>
ul>
div>
<div v-else>
<h2>没有信息哦h2>
div>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
info: {
name: "lili",
age: 12
}
}
}
})
app.mount("#lili")
script>
body>
<body>
<div id="lili">
<h1>你的分数:{{score}}h1>
<h2 v-if="score > 90">优秀h2>
<h2 v-else-if="score > 80">良好h2>
<h2 v-else-if="score >= 60">及格h2>
<h2 v-else>不及格h2>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
score: 9
}
}
})
app.mount("#lili")
script>
body>
从浏览器性能来说,多加一个元素需要多创建一个元素对象,原本的div没有存在的必要,从而提高了浏览器性能
vue3 才支持 template
类似于小程序的 block
只是为了对指令做一个应用,与 v-if 结合使用
如果 div 没有实际的意义,可以用 template 进行代替
<body>
<div id="lili">
<div> <button @click="toggle">切换button>div>
<template v-if="isShowCode">
<img src="https://game.gtimg.cn/images/yxzj/web201706/images/comm/floatwindow/wzry_qrcode.jpg" alt="">
template>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
message: "hello lili",
isShowCode: true
}
},
methods: {
toggle() {
this.isShowCode = !this.isShowCode
}
}
})
app.mount("#lili")
script>
body>
<div idli>
<h2>1. 电影列表h2>
<ul>
<li v-for="(item,index) in movies">{{index+1}}-{{item}}li>
ul>
<h2>2. 商品列表h2>
<div v-for="item in products" class="item">
<h3>商品:{{item.name}}h3>
<span>价格:{{item.price}}span>
<p>秒杀:{{item.desc}}p>
div>
div>
<body>
<div id="lili">
<h2>1. 电影列表h2>
<ul>
<li v-for="(item,index) in movies">{{index+1}}-{{item}}li>
ul>
<h2>2. 商品列表h2>
<div v-for="item in products" class="item">
<h3>商品:{{item.name}}h3>
<span>价格:{{item.price}}span>
<p>秒杀:{{item.desc}}p>
div>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
// 1. movies
movies: ["aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg"],
// 2. 数组中存放对象
products: [
{ id: 110, name: "MacBook", price: 9.99, desc: "赶紧来买" },
{ id: 111, name: "note", price: 9.49, desc: "赶买" },
{ id: 310, name: "phone", price: 3.99, desc: "来买" },
{ id: 530, name: "Mac", price: 9.94, desc: "赶来买" },
{ id: 450, name: "Book", price: 1.99, desc: "赶紧" },
],
message: "hello lili"
}
}
})
app.mount("#lili")
script>
body>
<body>
<div id="lili">
<ul>
<li v-for="(value,key,index) in info">{{value}}--{{key}}--{{index}}li>
ul>
<ul>
<li v-for="item in message">{{item}}li>
ul>
<ul>
<li v-for="item in 100">{{item}}li>
ul>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
message: "hello lili",
info: {
name: "lili",
age: 23,
height: 1.99
}
}
}
})
app.mount("#lili")
script>
body>
<script>
const app = Vue.createApp({
data() {
return {
message: "hello lili",
names: ["abc", "cba", "aaa", "bbb", "ccc"]
}
},
methods: {
changeArray() {
// 1. 直接将数组修改给一个新的数组
// this.names = ["why", "lili"]
// 2. 通过一些数组的方法,修改数组中的元素
// this.names.push("lili")
// this.names.pop()
// this.names.splice(2, 1, "hhh")
// this.names.sort()
// 3. 不修改原数组的方法不能监听(watch)
// this.names.map(item => item + "lili")
//需要重新赋值
const newInfos = this.names.map(item => item + "lili")
this.names = newInfos
}
}
})
app.mount("#lili")
script>
不只是一个简单的 div 而是有一大堆的元素,多个VNode应该会形成一个 VNode Tree
虚拟 dom 作用一:方便进行跨平台:通过虚拟dom根据不同渲染(浏览器、桌面端、移动端、VR设备)
虚拟 dom 作用二:可以进行diff虚拟算法
在模板中可以直接通过插值语法显示一些 data 中的数据
需要对多个 data 数据进行运算、三元运算符来决定结果、多个数据进行转换、结合起来进行显示
在模板中放入太多的逻辑会让模板阅读性不强,模板过重,难以维护
在真实开发里面,某一些数据,需要一系列操作之后才能显示出来,并不是直接显示出来,需要进行一系列的运算
将逻辑抽离出去:
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
message: "hello lili",
firstname: "lili",
lastname: "luelue"
}
},
computed: {
// 等同于
// 语法糖的写法
// fullname() {
// return this.firstname + " " + this.lastname
// },
// 完整写法
fullname: {
get: function () {
return this.firstname + " " + this.lastname
},
// 给计算属性设置值
set: function (value) {
const names = value.split(" ")
this.firstname = names[0]
this.middlename = names[1]
this.lastname = names[2]
}
}
},
methods: {
setFullname() {
this.fullname = "lili aiiii jiji"
}
}
})
app.mount("#lili")
script>
<script>
const app = Vue.createApp({
data() {
return {
message: "hello lili",
info: { name: "ysy", age: 22 }
}
},
methods: {
changeMessage() {
this.message = "你好啊",
this.info = { name: "lili", age: 19 }
}
},
watch: {
// 1. 默认有两个参数:newValue/oldValue
message(newValue, oldValue) {
console.log("message数据发生变化", newValue, oldValue);
},
info(newValue, oldValue) {
// 2. 对象返回的是proxy
console.log("info数据发生变化",newValue,oldValue);
// 3. 获取原始对象
// 3.1 迭代方式:新的对象
console.log({...newValue});
// 3.2 Vue.toRaw:原来的值
console.log(Vue.toRaw(newValue));
}
}
})
app.mount("#lili")
script>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
message: "hello lili",
info: {
name: "why", age: 12
}
}
},
methods: {
changeInfo() {
// 1. 创建一个新对象,赋值给info
// this.info = { name: "lili" }
// 2. 直接修改原对象的某一个属性
this.info.name = "lili"
}
},
watch: {
// info(newValue, oldValue) {
// 默认watch的监听是不会进行深度监听的
// console.log("侦听到info改变", newValue, oldValue);
// }
// 进行深度监听
info: {
handler(newValue, oldValue) {
// 用的还是同一个对象,改变的只是属性
console.log("侦听到info改变", newValue, oldValue);
console.log(newValue === oldValue);
},
// 监听器选项
// info进行深度监听
deep: true,
// 第一次渲染直接执行一次监听器,oldValue为undefined
immediate: true
},
// 单独监听一个属性
'info.name': function (newValue, oldValue) {
console.log(newValue, oldValue);
}
}
})
app.mount("#lili")
script>
<body>
<div id="lili">{{message}}
<button @click="changeMessage">修改messagebutton>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
message: "hello lili"
}
},
methods: {
changeMessage() {
this.message = "你好呀"
}
},
// 生命周期回调函数:当前组件被创建时自动执行
// 一般在该函数中,会进行网络请求
created() {
// ajax/fetch/axios
this.$watch("message", (newValue, oldValue) => {
console.log("message变化", newValue, oldValue);
}, {
deep: true
})
}
})
app.mount("#lili")
script>
body>
<style>
table {
border-collapse: collapse;
}
th,
td {
padding: 8px 10px;
text-align: center;
border: 1px solid red;
}
.active {
background-color: aquamarine;
}
style>
head>
<body>
<div id="lili">
<template v-if="books.length">
<table>
<thead>
<tr>
<th>序号th>
<th>书籍名称th>
<th>出版日期th>
<th>价格th>
<th>购买数量th>
<th>操作th>
tr>
thead>
<tbody>
<tr :class="{active:index===curIndex}" @mouseover="mouseover(index)" v-for="(item,index) in books"
:key="item.id">
<td>{{index+1}}td>
<td>{{item.name}}td>
<td>{{item.date}}td>
<td>${{item.price}}td>
<td><button :disabled="item.count<=1" @click="decrease(index,item)">-button>{{item.count}}<button
@click="increase(index)">+button>td>
<td><button @click="removeBook(index)">移除button>td>
tr>
tbody>
table>
<h3>总价:${{price}}h3>
template>
<template v-else>
<h2>快买h2>
template>
div>
<script src="../lib/vue.js">script>
<script src="./data.js">script>
<script>
const app = Vue.createApp({
data: function () {
return {
message: "hello lili",
books: books,
curIndex: -1
}
},
computed: {
price() {
let price = 0
for (const item of this.books) {
price += item.price * item.count
}
return price
},
},
methods: {
decrease(index, item) {
console.log("-", index);
let cur = this.books[index].count
if (cur > 1) this.books[index].count--
},
increase(index) {
console.log("-", index);
this.books[index].count++
},
removeBook(index) {
this.books.splice(index, 1)
},
mouseover(index) {
this.curIndex = index
}
}
})
app.mount("#lili")
script>
body>
<input v-model="searchText"/>
//等价于
<input :value="searchText" @input="searchText = $event.target.value"/>
<body>
<div id="lili">
<label for="account">
账号:<input id="account" type="text" v-model="account" />
label>
<label for="password">
密码:<input id="password" type="password" v-model="password" />
label>
<button @click="loginClick">登录button>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
// 每次绑定都得写属性
// username: "lili",
// password: '123456',
// message: "lilihhh"
account: "",
password: ""
}
},
methods: {
// 每次绑定都得写方法
userChange(event) {
this.username = event.target.value
},
loginClick() {
const account = this.account
const password = this.password
// url发送网络请求
console.log(account);
console.log(password);
}
}
})
app.mount("#lili")
script>
body>
<body>
<div id="lili">
<textarea cols="30" rows="10" v-model="content">textarea>
<p>输入的内容:{{content}}p>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
message: "hello lili",
content: "hhhhhhhhh"
}
}
})
app.mount("#lili")
script>
body>
<body>
<div id="lili">
<label for="agree">
<input id="agree" type="checkbox" v-model="isAgree">同意协议
label>
<h1>单选框:{{isAgree}}h1>
<div class="hobbies">
<h1>请选择你的爱好h1>
<label for="sing">
<input value="sing" type="checkbox" id="sing" v-model="hobbies">唱
label>
<label for="jump">
<input value="jump" type="checkbox" id="jump" v-model="hobbies">跳
label>
<h2>爱好:{{hobbies}}h2>
div>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
message: "hello lili",
isAgree: false,
hobbies: []
}
}
})
app.mount("#lili")
script>
body>
<div id="lili">
<div class="gender">
<label for="male">
<input id="male" type="radio" v-model="gender" value="男">男
label>
<label for="female">
<input id="female" type="radio" v-model="gender" value="女">女
label>
<h2>性别:{{gender}}h2>
div>
div>
<body>
<div id="lili">
<select v-model="fruit">
<option value="apple">苹果option>
<option value="orange">橘子option>
<option value="banana">香蕉option>
select>
<h2>单选:{{fruit}}h2>
<hr>
<select multiple size="3" v-model="fruits">
<option value="apple">苹果option>
<option value="orange">橘子option>
<option value="banana">香蕉option>
select>
<h2>多选:{{fruits}}h2>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
message: "hello lili",
fruit: "",
fruits: []
}
}
})
app.mount("#lili")
script>
body>
<body>
<div id="lili">
<select multiple size="3" v-model="fruits">
<option :value="item.value" v-for="item in allFruits" :key="item.value">{{item.text}}option>
select>
<h2>多选:{{fruits}}h2>
<div class="hobbies">
<h1>请选择你的爱好h1>
<template v-for="item in allHobbies" :key="item.value">
<label :for="item.value">
<input type="checkbox" :id="item.value" v-model="hobbies" :value="item.value">{{item.text}}
label>
template>
<h2>爱好:{{hobbies}}h2>
div>
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
allFruits: [
{ value: "apple", text: "苹果" },
{ value: "orange", text: "橘子" },
{ value: "banana", text: "香蕉" },
],
fruits: [],
// 爱好
allHobbies: [
{ value: "sing", text: "唱" },
{ value: "sing", text: "唱" },
{ value: "sing", text: "唱" },
],
hobbies: []
}
}
})
app.mount("#lili")
script>
body>
lazy
number
trim
使用多个修饰符
<body>
<div id="lili">
<input type="text" v-model.lazy="message">
<h1>message:{{message}}h1>
<input type="text" v-model.number="counter">
<h2>counter:{{counter}}--{{typeof counter}}h2>
<input type="number" v-model="counter2">
<input type="text" v-model.trim="content">
<h1>{{content}}h1>
<input type="text" v-model.trim.lazy="info">
div>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
message: "hello lili",
counter: 3,
counter2: 2,
content: "哈哈哈哈或",
info: "厉害啊"
}
},
watch: {
content(newValue) {
console.log(newValue);
}
}
})
app.mount("#lili")
script>
body>
<body>
<div id="lili">
<product-item>product-item>
<product-item>product-item>
<product-item>product-item>
div>
<script src="../lib/vue.js">script>
<script>
// 1. 组件:App组件(根组件)
const App = {
data() {
return {
title: "hello lili",
content: "我是内容哈哈哈哈"
}
}
}
// 2. 开发product-item组件
const productItem = {
template: `
{{title}}
{{content}}
商品图片
商品价格:¥9.9
商品描述信息,9.9秒杀
`
}//必须注册才能够使用
// 3. 创建app
const app = Vue.createApp(App)
// 4. 注册一个全局组件
app.component("product-item", productItem)
// 5. 挂载app
app.mount("#lili")
script>
body>
<body>
<div id="lili">
<product>product>
<product>product>
<product>product>
div>
<template id="items">
<div class="item">
<h2>woshishangpingh2>
<h3>shangpingtupianh3>
<h4>shangpingmiaoshuxinxih4>
div>
template>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp(App)
const App = {}
const productItem = {
template: "#items"
}
app.component("product", productItem)
app.mount("#lili")
script>
body>
app.component("product-item",productItem)
// 组件名称 具体的对象
<body>
<div id="lili">{{message}}
<home-nav>home-nav>
<product-item>product-item>
<product-item>product-item>
<product-item>product-item>
<product-item>product-item>
div>
<template id="product">
<div class="product">
<div>商品图片div>
<div>商品价格:<span>¥{{price}}span>div>
<p>商品描述信息,9.9秒杀p>
div>
<button @click="favorItem">收藏button>
template>
<template id="nav">
<div class="nav">
<h2>navvvvvvvvvvvvvvvvh2>
div>
template>
<script src="../lib/vue.js">script>
<script>
const app = Vue.createApp({
data() {
return {
message: "hello lili"
}
}
})
// 注册全局组件
app.component("product-item", {
template: "#product",
data() {
return {
title: "我是商品的item",
price: 99
}
},
methods: {
favorItem() {
console.log("点击了收藏");
}
}
})
app.component("home-nav", {
template: "#nav"
})
app.mount("#lili")
script>
body>
<body>
<div id="lili">{{message}}
<product-item>product-item>
<home-nav>
home-nav>
div>
<template id="product">
<div class="product">
<div>商品图片div>
<div>商品价格:<span>¥{{price}}span>div>
<p>商品描述信息,9.9秒杀p>
div>
template>
<template id="nav">
<div class="nav">
<h2>nnnnnnnnnnnnnnnnnnnavvvvvvvvvvvvvvvvh2>
<product-item>product-item>
div>
template>
<script src="../lib/vue.js">script>
<script>
const ProductItem = {
template: "#product",
data() {
return {
price: 99
}
}
}
const app = Vue.createApp({
data() {
return {
message: "hello lili"
}
},
// 告诉vue在这个组件里面会用到哪些别的组件
components: {
ProductItem
,
HomeNav: {
template: "#nav",
components: {
ProductItem
}
}
}
})
app.mount("#lili")
script>
body>
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"baseUrl": "./",
"moduleResolution": "node",
"paths": {
"@/*": ["src/*"],
"utils/*": ["src/utils/*"]
},
"lib": ["esnext", "dom", "dom.iterable", "scripthost"]
}
}
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
transpileDependencies: true,
configureWebpack: {
resolve: {
// 配置路径别名:@是已经配置好的路径别名,对应的是src路径
alias: {
'utils':"@/utils"
}
}
}
})
做法一: App.vue文件 SFC
做法二: 自己定义了一个对象:template/data/methods
Vue CLI:vue create
npm init vue@latest