vue学习

This commit is contained in:
yingfeng.ai 2024-10-31 17:55:51 +08:00
parent be69ea3ba4
commit b4ee6e0525
11 changed files with 240 additions and 57 deletions

View File

@ -1,20 +1,14 @@
<script setup lang="ts">
import Person from './components/Person.vue'
</script>
<template>
<header>
<img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" />
<div class="wrapper">
<h1>你好啊</h1>
</div>
</header>
<main>
<Person />
<Person a="haha" />
</main>
</template>
<script setup lang="ts">
import Person from './components/Person.vue'
</script>
<style scoped>
header {
line-height: 1.5;

View File

@ -1,57 +1,26 @@
<template>
<div class="person">
<h2>姓名{{ person.name }}</h2>
<h2>年龄{{ person.age }}</h2>
<h2>汽车{{ person.car.c1 }}-{{ person.car.c2 }}</h2>
<button @click="changeName">修改名字</button>
<button @click="changeAge">修改年龄</button>
<button @click="changeCar">修改车</button>
<img v-for="(dog, index) in dogList" :src="dog" :key="index">
<button @click="changeImg">点击换图</button>
</div>
</template>
<script setup>
import {reactive, watch} from "vue";
<script setup lang="ts">
import {ref, reactive} from 'vue'
import axios from "axios";
let dogList = reactive([
'https://images.dog.ceo/breeds/spaniel-irish/n02102973_2902.jpg'
])
let person = reactive({
name: '张三',
age: 18,
car: {
c1: '奔驰',
c2: '宝马',
async function changeImg() {
try{
let res = await axios.get("https://dog.ceo/api/breeds/image/random")
dogList.push(res.data.message)
} catch (e) {
alert(e)
}
})
function changeName() {
person.name += '~~~'
}
function changeAge() {
person.age += 1
}
function changeCar() {
person.car.c1 = "雅迪"
}
//
watch(() => {
return person.name
}, (nValue, oValue) => {
console.log(nValue, oValue)
}, {deep: true})
//
watch(() => person.car, (nValue, oValue) => {
console.log(nValue, oValue)
}, {deep: true})
//
watch([() => person.car, () => person.name], (nValue, oValue) => {
console.log(nValue, oValue)
}, {deep: true})
</script>
@ -64,4 +33,8 @@ watch([() => person.car, () => person.name], (nValue, oValue) => {
button {
margin: 5px;
}
img {
height: 100px;
}
</style>

View File

View File

View File

@ -0,0 +1,9 @@
// 定义一个接口用于限制person对象的具体属性
export interface PersonInter {
id: string,
name: string,
age: number
}
// 自定义类型
export type Persons = Array<PersonInter>

View File

@ -0,0 +1,35 @@
<template>
<div class="person">
<h2>{{ a }}</h2>
<h2>{{ list }}</h2>
<ul>
<li v-for="item in list" :key="item.id">
{{ item.name }} -- {{ item.age }} -- {{ item.id }}
</li>
</ul>
</div>
</template>
<script setup lang="ts">
import {defineProps, withDefaults} from 'vue'
import {type Persons} from '@/types'
// aa. list
const props = defineProps<{
a: string; // a
list: Persons; // list Persons
}>()
</script>
<style scoped>
.person {
background-color: antiquewhite;
box-shadow: 0 0 10px;
}
button {
margin: 5px;
}
</style>

View File

@ -0,0 +1,47 @@
<template>
<main>
<Person a="haha" :list="PersonList"/>
</main>
</template>
<script setup lang="ts">
import Person from './components/Person.vue'
import {reactive} from 'vue'
import {type Persons} from '@/types'
let PersonList = reactive<Persons>([
{id: 'ayf0001', name: '张一', age: 18},
{id: 'ayf0002', name: '张二', age: 19},
{id: 'ayf0003', name: '张三', age: 17}
])
</script>
<style scoped>
header {
line-height: 1.5;
}
.logo {
display: block;
margin: 0 auto 2rem;
}
@media (min-width: 1024px) {
header {
display: flex;
place-items: center;
padding-right: calc(var(--section-gap) / 2);
}
.logo {
margin: 0 2rem 0 0;
}
header .wrapper {
display: flex;
place-items: flex-start;
flex-wrap: wrap;
}
}
</style>

View File

@ -0,0 +1,35 @@
<template>
<div class="person">
<h1>中国</h1>
<h2 ref="title2">苏州</h2>
<button @click="showLog">点我输出</button>
</div>
</template>
<script setup>
import {ref, defineExpose} from "vue";
// title2ref
let title2 = ref()
let a = ref()
let b = ref()
let c = ref()
function showLog(){
console.log(title2.value)
}
defineExpose({a, b, c})
</script>
<style scoped>
.person {
background-color: antiquewhite;
box-shadow: 0 0 10px;
}
button {
margin: 5px;
}
</style>

View File

@ -0,0 +1,48 @@
<script setup lang="ts">
import Person from './components/Person.vue'
import {ref} from "vue";
// renref
let ren = ref()
function showLog(){
console.log(ren.value)
}
</script>
<template>
<main>
<Person ref="ren" />
<button @click="showLog">点ren我输出</button>
</main>
</template>
<style scoped>
header {
line-height: 1.5;
}
.logo {
display: block;
margin: 0 auto 2rem;
}
@media (min-width: 1024px) {
header {
display: flex;
place-items: center;
padding-right: calc(var(--section-gap) / 2);
}
.logo {
margin: 0 2rem 0 0;
}
header .wrapper {
display: flex;
place-items: flex-start;
flex-wrap: wrap;
}
}
</style>

View File

@ -0,0 +1,9 @@
// 定义一个接口用于限制person对象的具体属性
export interface PersonInter {
id: string,
name: string,
age: number
}
// 自定义类型
export type Persons = Array<PersonInter>

View File

@ -0,0 +1,33 @@
<template>
<div class="person">
</div>
</template>
<script setup>
import {type PersonInter, type Persons} from '@/types'
//
let person: PersonInter = {id: 'ayf0001', name: '张三', age: 18}
//
let personList: Persons = [
{id: 'ayf0001', name: '张一', age: 18},
{id: 'ayf0002', name: '张二', age: 19},
{id: 'ayf0003', name: '张三', age: 17}
]
</script>
<style scoped>
.person {
background-color: antiquewhite;
box-shadow: 0 0 10px;
}
button {
margin: 5px;
}
</style>