Vue3组件开发-GlobalHeader

这个组件比上面一个组件简单多了,但是我在写的时候还是遇到了奇奇怪怪的问题。

1 环境

主要用到的环境是VScode+TS+VUE3+Bootstrap

这个就是一个brandbar,头部导航条,逻辑方面就是判断是否登录,如果登录了那么就显示XXX欢迎,如果是未登录那么就是,登录以及注册的按钮跳转。

2 代码

2.1 GlobalHeader组件:

代码:

//创建一个globalheader组件
<template>
    <nav class="navbar navbar-dark bg-primary justify-content-between mb-4 px-4">
    <a class="navbar-borand" href="#" :style="{color :'black', fontSize: '18px'}">者也专栏</a>
    <ul v-if="!user.isLogin" class="list-inline mb-0">
        <li class="list-inline-item"><a href="#" class="btn-outline-light my-2 " :style="{color :'black', fontSize: '18px'}">注册</a></li>
        <li class="list-inline-item"><a href="#" class="btn-outline-light my-2" :style="{color :'black', fontSize: '18px'}">登录</a></li>
    </ul>
    <ul v-else class="list-inline mb-0">
        <li class="list-inline-item"><a href="#" class="btn btn-outline-light my-2" :style="{color :'black', fontSize: '18px'}">你好{{user.name}}</a></li>
    </ul>
    </nav>
</template>
<script lang="ts">
import { defineComponent, PropType } from 'vue'
export interface UserProps {
    isLogin:boolean;
    name?:string;
    id?:number;
}
export default defineComponent({
  name: 'GlobalHeader',
  props: {
    user: {
      type: Object as PropType<UserProps>,
      required: true

    }
  }

})
</script>

2.2数据:

App.vue

代码:

const currentUser: UserProps = {
  // isLogin: false
  isLogin: true,
  name: 'aFang'

}

加上上次的:

<template>
<div class="container">
  <global-haeder :user="currentUser"></global-haeder>
  <column-list :list="list"></column-list>
</div>
</template>

<script lang="ts">
import {
  defineComponent
} from 'vue'
import 'bootstrap/dist/css/bootstrap.min.css' // 引入bootstrap
import ColumnList, {
  ColumnProps
} from './components/ColumnList.vue'
import GlobalHaeder, { UserProps } from './components/GlobalHeader.vue'
const currentUser: UserProps = {
  // isLogin: false
  isLogin: true,
  name: 'aFang'

}
const testData: ColumnProps[] = [{
  id: 1,
  title: 'test1的专栏',
  description: '知乎专栏的描述',
  avatar: 'http://vue-maker.oss-cn-hangzhou.aliyuncs.com/vue-marker/5ee22dd58b3c4520912b9470.jpg?x-oss-process=image/resize,m_pad,h_100,w_100'

}, {
  id: 2,
  title: 'test2的专栏',
  description: '知乎专栏的描述',
  avatar: 'http://vue-maker.oss-cn-hangzhou.aliyuncs.com/vue-marker/5ee22dd58b3c4520912b9470.jpg?x-oss-process=image/resize,m_pad,h_100,w_100'

}, {
  id: 3,
  title: 'test3的专栏',
  description: '知乎专栏的描述'

}, {
  id: 4,
  title: 'test4的专栏',
  description: '知乎专栏的描述',
  avatar: ''

}]
export default defineComponent({
  name: 'App',
  components: {
    ColumnList,
    GlobalHaeder
  },
  setup () {
    return {
      list: testData,
      currentUser
    }
  }
})
</script>

<style>

</style>

3 运行

终端输入命令:

npm run serve

运行效果

  1. 登录

    image-20220113004333975

  2. 未登录

    image-20220113004426331

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值