
批改狀態(tài):合格
老師批語:
<template>
<div>歐陽克</div>
<OyDiv style="color:red;" size="2" type="wrning">歐陽組件</OyDiv>
<Ztp></Ztp>
</template>
<script>
import OyDiv from "./components/OyDiv.vue";
export default{
// data:數(shù)據(jù); methods:js計算屬性等;
// components:組件; 使用組件,傳值
components:{
OyDiv
},
}
<template>
<div>{{ size }}</div>
<div>{{ type }}</div>
</template>
<script>
export default({
// 1.組件里面的api都是和頁面文件一樣的
// props:配置項,是接收傳值的
// props:接收的值,直接跟data里的數(shù)據(jù)一樣使用
props: ['size', 'type']
})
</script>
<template>
<div>歐陽克</div>
<!--
<oy-div style="color:red;" size="2">歐陽組件</oy-div>
<OyDiv style="color:red;" size="2">歐陽組件</OyDiv>
-->
<OyDiv style="color:red;" size="2" type="wrning">歐陽組件</OyDiv>
<Ztp></Ztp>
</template>
<template>
<div>朱天蓬組件</div>
</template>
<template>
<div>歐陽克</div>
<OyDiv style="color:red;" size="2" type="wrning">歐陽組件</OyDiv>
</template>
<script>
import OyDiv from "./components/OyDiv.vue";
export default{
// data:數(shù)據(jù); methods:js計算屬性等;
// components:組件; 使用組件,傳值
components:{
OyDiv
},
}
<template>
<div>{{ size }}</div>
<div>{{ type }}</div>
<Ztp></Ztp>
</template>
<script>
import Ztp from "./Ztp.vue";
export default({
// 1.組件里面的api都是和頁面文件一樣的
// props:配置項,是接收傳值的
// props:接收的值,直接跟data里的數(shù)據(jù)一樣使用
props: ['size', 'type'],
components: {
Ztp
}
})
</script>
<template>
<div>歐陽克</div>
<OyDiv style="color:red;" :arr="arr_v" types="wrning">歐陽組件</OyDiv>
</template>
<script>
import OyDiv from "./components/OyDiv.vue";
export default{
// data:數(shù)據(jù); methods:js計算屬性等;
// components:組件; 使用組件,傳值
components:{
OyDiv
},
data(){
return {
arr_v:[
"歐陽克",
"朱天蓬"
],
}
},
}
<template>
<div>{{ size }}</div>
<div>{{ types }}</div>
<div>{{ arr }}</div>
<Ztp></Ztp>
</template>
<script>
import Ztp from "./Ztp.vue";
export default({
// 1.組件里面的api都是和頁面文件一樣的
// props:配置項,是接收傳值的
// props:接收的值,直接跟data里的數(shù)據(jù)一樣使用
props: {
size: {
type : Number,
required: true,
default: 0
},
types: {
type : String
},
arr : {
type : Array
}
},
components: {
Ztp
}
})
</script>
<template>
{{ size }}
{{ type }}
<button v-if="type==''" class="bottom">按鈕</button>
<button v-else-if="type=='primary'" class="bottom oy-button--primary">按鈕</button>
</template>
<script>
export default({
props: {
size: {
},
type: {
},
},
})
</script>
<style scoped>
button { xxx }
.oy-button--primary { xxx }
<style>
<template>
<div>歐陽克</div>
<!--<OyDiv style="color:red;" :arr="arr_v" types="wrning">歐陽組件</OyDiv>-->
<OyButton size="large" type=""></OyButton>
</template>
<script>
import OyDiv from "./components/OyDiv.vue";
import OyButton from "./components/OyButton.vue";
export default{
// data:數(shù)據(jù); methods:js計算屬性等;
// components:組件; 使用組件,傳值
components:{
OyDiv,
OyButton
},
}
</script>
<OyButton size="large" type="primary"></OyButton>
App.vue文件
<template>
<div>歐陽克</div>
<!--插槽-->
<OyButton size="large" type="primary">提交</OyButton>
</template>
OnButton.vue文件
<template>
{{ size }}
{{ type }}
<button class="bottom">
<!-- 添加一個插槽 -->
<!-- 插槽可以在組件的任意位置 -->
<slot></slot>
</button>
<button v-if="type==''" class="bottom">按鈕</button>
<button v-else-if="type=='primary'" class="bottom oy-button--primary">按鈕</button>
</template>
<!--插槽-->
<OyButton size="large" type="primary">
提交
<span style="color:red">確定</span>
<!-- 用標簽來使用有名字的插槽 -->
<template v-slot:one>
<div style="color:greenyellow">確定</div>
</template>
<!-- 語法糖 # -->
<template #two>
<div style="color:blue">確定</div>
</template>
</OyButton>
<template>
{{ size }}
{{ type }}
<button class="bottom">
<!-- 添加一個插槽 -->
<!-- 插槽可以在組件的任意位置 -->
<slot></slot>
</button>
<slot name="one"></slot>
<slot name="two"></slot>
<button v-if="type==''" class="bottom">按鈕</button>
<button v-else-if="type=='primary'" class="bottom oy-button--primary">按鈕</button>
</template>
<template>
<OyJs></OyJs>
</template>
<script>
import OyJs from "./components/OyJs.vue";
export default{
components:{
OyJs
},
data(){
return {
}
},
methods : {
fun(e){
console.log('父組件方法');
console.log(e);
}
},
}
</script>
<template>
<div>OyJs</div>
<div>{{ OyJs_fun() }}</div>
<button @click="OyJs_fun(1)">按鈕</button>
</template>
<script>
export default({
props: {
},
methods: {
OyJs_fun(e){
console.log('OyJs方法');
// 使用全局屬性
// $parent 上一級,父級方法
this.$parent.fun(e);
// 最頂層,根目錄
this.$root.fun(e);
}
}
})
</script>
this.$parent.fun: $parent 上一級,父級方法
this.$root.fun: $root 最頂層,根目錄
聲明周期 | 作用 |
---|---|
beforeCreate | 在創(chuàng)建組件之前調(diào)用 |
created | 組件創(chuàng)建完成調(diào)用 |
beforeMount | 模板掛載之前調(diào)用 |
mounted | 模板掛載完成調(diào)用 |
beforeUpdate | 改變內(nèi)容之前調(diào)用 |
update | 改變內(nèi)容之后調(diào)用 |
beforeUnmout | 組件銷毀之前調(diào)用 |
unmounted | 組件銷毀之后調(diào)用 |
路由文件路徑:src/router/index.js
視圖文件路徑:src/view/XxXxx.vue
import MyView from '../views/MyView.vue'
routes: [
{
path: '/my',
name: 'my',
component: MyView
},
]
<router-link to = "/">首頁</router-link> |
<router-link to ="/about">幫助頁面</router-link> |
<router-link to = "/my">個人中心</router-link>
在頁面內(nèi)刷新跳轉(zhuǎn)新頁面,不用怎個頁面刷新
<el-button @click="go_url()">跳轉(zhuǎn)</el-button>
<script>
methods : {
},
go_url(){
// 路由有個全局變量
// 用選項api,都是this. 可以訪問全局變量
// this.$route 獲取頁面信息
this.$router.push('/my');
}
},
}
</script>
路由跳轉(zhuǎn)不支持跳轉(zhuǎn)至外網(wǎng),如需要可用a標簽
this.$router.push:是跳轉(zhuǎn)到新頁面
this.$route:是獲取頁面信息
go_url(){
// 路由有個全局變量
// 用選項api,都是this. 可以訪問全局變量
// this.$route 獲取頁面信息
//this.$router.push('/my');
console.log(this.$route);
// 用js跳轉(zhuǎn),增加傳值
this.$router.push({
path : '/my',
query : {
id:188,
oid:288
}
});
4.2 新建路由:
修改路由文件 index.js
子路由的path,不能在前面加反斜杠/
import MyOrderView from '../views/MyOrderView.vue'
import MyConfigView from '../views/MyConfigView.vue'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/my',
name: 'my',
component: MyView,
// 子路由的path,不能在前面加反斜杠/
children: [
{
path: 'order',
name: 'my_order',
component: MyOrderView,
},
{
path: 'config',
name: 'my_config',
component: MyConfigView
},
]
},
]
})
<template>
<div>
<h1>這是個人中心頁面</h1>
<router-link to="/my/order">訂單頁面</router-link> |
<router-link to="/my/config">配置頁面</router-link>
<router-view></router-view>
</div>
</template>
<template>
<div><h1>這是個人中心配置頁面</h1></div>
</template>
<template>
<div><h1>這是個人中心訂單頁面</h1></div>
</template>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號