亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 77
粉絲 0
評論 0
訪問量 80561
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
Vue3組件的傳值、插槽,Vue3新路由和子路由配置和使用
Jet的博客
原創(chuàng)
2916人瀏覽過

一、Vue3組件的傳值

1、傳值

  • App.vue文件
  1. <template>
  2. <div>歐陽克</div>
  3. <OyDiv style="color:red;" size="2" type="wrning">歐陽組件</OyDiv>
  4. <Ztp></Ztp>
  5. </template>
  6. <script>
  7. import OyDiv from "./components/OyDiv.vue";
  8. export default{
  9. // data:數(shù)據(jù); methods:js計算屬性等;
  10. // components:組件; 使用組件,傳值
  11. components:{
  12. OyDiv
  13. },
  14. }
  • OyDiv.vue組件文件(組件文件)
  1. <template>
  2. <div>{{ size }}</div>
  3. <div>{{ type }}</div>
  4. </template>
  5. <script>
  6. export default({
  7. // 1.組件里面的api都是和頁面文件一樣的
  8. // props:配置項,是接收傳值的
  9. // props:接收的值,直接跟data里的數(shù)據(jù)一樣使用
  10. props: ['size', 'type']
  11. })
  12. </script>


2、可以多個組件

  • App.vue文件
  1. <template>
  2. <div>歐陽克</div>
  3. <!--
  4. <oy-div style="color:red;" size="2">歐陽組件</oy-div>
  5. <OyDiv style="color:red;" size="2">歐陽組件</OyDiv>
  6. -->
  7. <OyDiv style="color:red;" size="2" type="wrning">歐陽組件</OyDiv>
  8. <Ztp></Ztp>
  9. </template>
  • Ztp.vue文件(組件文件)
  1. <template>
  2. <div>朱天蓬組件</div>
  3. </template>


3、組件引入多個子組件

  • App.vue組件文件
  1. <template>
  2. <div>歐陽克</div>
  3. <OyDiv style="color:red;" size="2" type="wrning">歐陽組件</OyDiv>
  4. </template>
  5. <script>
  6. import OyDiv from "./components/OyDiv.vue";
  7. export default{
  8. // data:數(shù)據(jù); methods:js計算屬性等;
  9. // components:組件; 使用組件,傳值
  10. components:{
  11. OyDiv
  12. },
  13. }
  • Ztp.vue子組件
  1. <template>
  2. <div>{{ size }}</div>
  3. <div>{{ type }}</div>
  4. <Ztp></Ztp>
  5. </template>
  6. <script>
  7. import Ztp from "./Ztp.vue";
  8. export default({
  9. // 1.組件里面的api都是和頁面文件一樣的
  10. // props:配置項,是接收傳值的
  11. // props:接收的值,直接跟data里的數(shù)據(jù)一樣使用
  12. props: ['size', 'type'],
  13. components: {
  14. Ztp
  15. }
  16. })
  17. </script>


4、傳值的配置項

  • app.vue文件
  1. <template>
  2. <div>歐陽克</div>
  3. <OyDiv style="color:red;" :arr="arr_v" types="wrning">歐陽組件</OyDiv>
  4. </template>
  5. <script>
  6. import OyDiv from "./components/OyDiv.vue";
  7. export default{
  8. // data:數(shù)據(jù); methods:js計算屬性等;
  9. // components:組件; 使用組件,傳值
  10. components:{
  11. OyDiv
  12. },
  13. data(){
  14. return {
  15. arr_v:[
  16. "歐陽克",
  17. "朱天蓬"
  18. ],
  19. }
  20. },
  21. }
  • OyDiv.vue文件
  1. <template>
  2. <div>{{ size }}</div>
  3. <div>{{ types }}</div>
  4. <div>{{ arr }}</div>
  5. <Ztp></Ztp>
  6. </template>
  7. <script>
  8. import Ztp from "./Ztp.vue";
  9. export default({
  10. // 1.組件里面的api都是和頁面文件一樣的
  11. // props:配置項,是接收傳值的
  12. // props:接收的值,直接跟data里的數(shù)據(jù)一樣使用
  13. props: {
  14. size: {
  15. type : Number,
  16. required: true,
  17. default: 0
  18. },
  19. types: {
  20. type : String
  21. },
  22. arr : {
  23. type : Array
  24. }
  25. },
  26. components: {
  27. Ztp
  28. }
  29. })
  30. </script>


5、判斷值賦予樣式

  • OyButton.vue文件
  1. <template>
  2. {{ size }}
  3. {{ type }}
  4. <button v-if="type==''" class="bottom">按鈕</button>
  5. <button v-else-if="type=='primary'" class="bottom oy-button--primary">按鈕</button>
  6. </template>
  7. <script>
  8. export default({
  9. props: {
  10. size: {
  11. },
  12. type: {
  13. },
  14. },
  15. })
  16. </script>
  17. <style scoped>
  18. button { xxx }
  19. .oy-button--primary { xxx }
  20. <style>

  • App.vue文件
  1. <template>
  2. <div>歐陽克</div>
  3. <!--<OyDiv style="color:red;" :arr="arr_v" types="wrning">歐陽組件</OyDiv>-->
  4. <OyButton size="large" type=""></OyButton>
  5. </template>
  6. <script>
  7. import OyDiv from "./components/OyDiv.vue";
  8. import OyButton from "./components/OyButton.vue";
  9. export default{
  10. // data:數(shù)據(jù); methods:js計算屬性等;
  11. // components:組件; 使用組件,傳值
  12. components:{
  13. OyDiv,
  14. OyButton
  15. },
  16. }
  17. </script>


  1. <OyButton size="large" type="primary"></OyButton>


@style scoped

  • scoped:可以穿透到其他文件使用,或者本文件單獨使用

6、插槽的使用方式:

  • 單個插槽

  • App.vue文件

    1. <template>
    2. <div>歐陽克</div>
    3. <!--插槽-->
    4. <OyButton size="large" type="primary">提交</OyButton>
    5. </template>
  • OnButton.vue文件

  1. <template>
  2. {{ size }}
  3. {{ type }}
  4. <button class="bottom">
  5. <!-- 添加一個插槽 -->
  6. <!-- 插槽可以在組件的任意位置 -->
  7. <slot></slot>
  8. </button>
  9. <button v-if="type==''" class="bottom">按鈕</button>
  10. <button v-else-if="type=='primary'" class="bottom oy-button--primary">按鈕</button>
  11. </template>


  • 多個插槽

  • App.vue文件

  1. <!--插槽-->
  2. <OyButton size="large" type="primary">
  3. 提交
  4. <span style="color:red">確定</span>
  5. <!-- 用標簽來使用有名字的插槽 -->
  6. <template v-slot:one>
  7. <div style="color:greenyellow">確定</div>
  8. </template>
  9. <!-- 語法糖 # -->
  10. <template #two>
  11. <div style="color:blue">確定</div>
  12. </template>
  13. </OyButton>
  • OnButton.vue文件
  1. <template>
  2. {{ size }}
  3. {{ type }}
  4. <button class="bottom">
  5. <!-- 添加一個插槽 -->
  6. <!-- 插槽可以在組件的任意位置 -->
  7. <slot></slot>
  8. </button>
  9. <slot name="one"></slot>
  10. <slot name="two"></slot>
  11. <button v-if="type==''" class="bottom">按鈕</button>
  12. <button v-else-if="type=='primary'" class="bottom oy-button--primary">按鈕</button>
  13. </template>


7、子組件調(diào)用父組件方法

  • App.vue文件
  1. <template>
  2. <OyJs></OyJs>
  3. </template>
  4. <script>
  5. import OyJs from "./components/OyJs.vue";
  6. export default{
  7. components:{
  8. OyJs
  9. },
  10. data(){
  11. return {
  12. }
  13. },
  14. methods : {
  15. fun(e){
  16. console.log('父組件方法');
  17. console.log(e);
  18. }
  19. },
  20. }
  21. </script>
  • OyJs.vue文件
  1. <template>
  2. <div>OyJs</div>
  3. <div>{{ OyJs_fun() }}</div>
  4. <button @click="OyJs_fun(1)">按鈕</button>
  5. </template>
  6. <script>
  7. export default({
  8. props: {
  9. },
  10. methods: {
  11. OyJs_fun(e){
  12. console.log('OyJs方法');
  13. // 使用全局屬性
  14. // $parent 上一級,父級方法
  15. this.$parent.fun(e);
  16. // 最頂層,根目錄
  17. this.$root.fun(e);
  18. }
  19. }
  20. })
  21. </script>


this.$parent.fun: $parent 上一級,父級方法
this.$root.fun: $root 最頂層,根目錄


8、組件生命周期

聲明周期 作用
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)用

二、Vue3路由

  • 路由文件路徑:src/router/index.js

  • 視圖文件路徑:src/view/XxXxx.vue

1、新增路由:

  • 1.1 創(chuàng)建視圖文件:
  • 1.2 新建路由:
  1. import MyView from '../views/MyView.vue'
  2. routes: [
  3. {
  4. path: '/my',
  5. name: 'my',
  6. component: MyView
  7. },
  8. ]


2、單頁面跳轉(zhuǎn)

  1. <router-link to = "/">首頁</router-link> |
  2. <router-link to ="/about">幫助頁面</router-link> |
  3. <router-link to = "/my">個人中心</router-link>

在頁面內(nèi)刷新跳轉(zhuǎn)新頁面,不用怎個頁面刷新


3、另外一個router跳轉(zhuǎn)方法

  • app.vue文件
  1. <el-button @click="go_url()">跳轉(zhuǎn)</el-button>
  1. <script>
  2. methods : {
  3. },
  4. go_url(){
  5. // 路由有個全局變量
  6. // 用選項api,都是this. 可以訪問全局變量
  7. // this.$route 獲取頁面信息
  8. this.$router.push('/my');
  9. }
  10. },
  11. }
  12. </script>

路由跳轉(zhuǎn)不支持跳轉(zhuǎn)至外網(wǎng),如需要可用a標簽
this.$router.push:是跳轉(zhuǎn)到新頁面
this.$route:是獲取頁面信息


自行配置跳轉(zhuǎn)參數(shù)

  1. go_url(){
  2. // 路由有個全局變量
  3. // 用選項api,都是this. 可以訪問全局變量
  4. // this.$route 獲取頁面信息
  5. //this.$router.push('/my');
  6. console.log(this.$route);
  7. // 用js跳轉(zhuǎn),增加傳值
  8. this.$router.push({
  9. path : '/my',
  10. query : {
  11. id:188,
  12. oid:288
  13. }
  14. });


4、子路由

  • 4.1 創(chuàng)建視圖文件:
  • 4.2 新建路由:

  • 修改路由文件 index.js

子路由的path,不能在前面加反斜杠/

  1. import MyOrderView from '../views/MyOrderView.vue'
  2. import MyConfigView from '../views/MyConfigView.vue'
  3. const router = createRouter({
  4. history: createWebHistory(import.meta.env.BASE_URL),
  5. routes: [
  6. {
  7. path: '/my',
  8. name: 'my',
  9. component: MyView,
  10. // 子路由的path,不能在前面加反斜杠/
  11. children: [
  12. {
  13. path: 'order',
  14. name: 'my_order',
  15. component: MyOrderView,
  16. },
  17. {
  18. path: 'config',
  19. name: 'my_config',
  20. component: MyConfigView
  21. },
  22. ]
  23. },
  24. ]
  25. })
  • 修改附近視圖文件 MyView.vue
  1. <template>
  2. <div>
  3. <h1>這是個人中心頁面</h1>
  4. <router-link to="/my/order">訂單頁面</router-link> |
  5. <router-link to="/my/config">配置頁面</router-link>
  6. <router-view></router-view>
  7. </div>
  8. </template>
  • 增加子路由文件:MyConfigView.vue、MyOrderView.vue
  1. <template>
  2. <div><h1>這是個人中心配置頁面</h1></div>
  3. </template>
  1. <template>
  2. <div><h1>這是個人中心訂單頁面</h1></div>
  3. </template>

批改老師:PHPzPHPz

批改狀態(tài):合格

老師批語:
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
作者最新博文
關(guān)于我們 免責申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學習者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學習
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學習!
    全站2000+教程免費學