html%3cli%3e选项图标,从零开始打造一个组件库–1.编写一个时间线组件

本文档详细介绍了如何从零开始搭建Vue.js项目环境,包括使用npm初始化,安装依赖,配置webpack和babel,处理ES6语法,以及搭建vue-cli。接着,文章深入解析了一个时间线组件的结构,划分了组件的各个部分,并逐步实现了组件的静态样式和逻辑封装。通过provide/inject API实现了父子组件的状态共享,并使用jsx语法处理组件渲染。最后,文章展示了如何在项目中使用这些组件,验证了组件功能的实现。

一.搭建开发环境

第一步,让我们先把项目环境搭建好,首先打开命令窗口,执行如下命令:

npm init

搭建好了package.json文件之后,接下来开始装依赖包,我们需要用到webpack webpack-cli来打包项目,执行如下命令:

npm install webpack webpack-cli --save-dev

在编写代码时,我们需要用到es6的语法,因此我们还需要安装@babel/core @babel/cli @babel/preset-env babel-loader依赖来处理es6兼容语法。继续执行如下命令:

npm install --save-dev @babel/core @babel/cli @babel/preset-env babel-loader

接下来,创建一个babel.config.json文件,然后写入如下代码:

{

"presets": [

[

"@babel/env",

{

"targets": {

"edge": "17",

"firefox": "60",

"chrome": "67",

"safari": "11.1",

},

"useBuiltIns": "usage",

}

]

]

}

这只是一个默认的配置,也可以自行根据需求来进行配置,更多信息详见babel文档。

这还没有结束,我们还需要搭建vue的开发环境,我们需要编译.vue,所以我们需要安装vue-loader vue-template-compiler vue等依赖包,继续执行如下命令:

npm install vue vue-loader vue-template-compiler --save-dev

我们目前所需要的依赖就暂时搭建完成,接下来在页面根目录创建一个index.html文件,写入如下代码:

vue-cli

在这里,我们注意到了我们打包最后引入的文件为build.js文件,接下来我们开始编写webpack的配置,在根目录下继续创建一个webpack.config.js文件,代码如下:

const VueLoaderPlugin = require('vue-loader/lib/plugin');

module.exports = {

mode:"development",

entry:'./main.js',

output:{

path:__dirname,

filename:'build.js'

},

module:{

rules:[

{

test: /\.vue$/,

loader: "vue-loader"

},

{

test: /\.js$/,

loader: "babel-loader",

exclude: /node_modules/

}

]

},

plugins: [

new VueLoaderPlugin()

]

}

在导出后面还需要加上这一段js代码,如下:

resolve: {

alias: {

'vue': 'vue/dist/vue.js'

}

}

为什么要加上这一个配置,这个后面会说明原因,这里暂时先放置,继续在根目录下分别创建一个App.vue与main.js文件。代码分别如下:

import Vue from 'vue';

import App from './App.vue'

Vue.config.productionTip = false;

var vm = new Vue({

el: "#app",

// render:(h) => { return h(App)},

components: {

App

},

template: ""

})

{{ msg }}

export default {

data() {

return {

msg: "hello,vue.js!"

};

},

mounted() {

},

methods: {

}

};

接下来,执行命令webpack,然后我们就可以看到页面中会生成一个build.js文件,然后运行index.html文件,我们就可以在浏览器页面上看到hello,vue.js!的字符串,稍等,我们似乎忘记了什么,一般在开发中,谁会给你运行webpack命令来打包,不都是执行npm run build嘛,让我们在package.json中加上这一行代码

{

"name": "timeline-project",

"version": "1.0.0",

"description": "a component with vue.js",

"main": "index.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1",

"build": "webpack" //这里是添加的代码

},

"keywords": [

"timeline"

],

"author": "",

"license": "ISC",

"devDependencies": {

"@babel/cli": "^7.11.5",

"@babel/core": "^7.11.5",

"@babel/preset-env": "^7.11.5",

"babel-loader": "^8.1.0",

"vue": "^2.6.12",

"vue-loader": "^15.9.3",

"vue-template-compiler": "^2.6.12",

"webpack": "^4.44.1",

"webpack-cli": "^3.3.12"

}

}

等等,我们还忘了一件事,别人都可以使用npm run dev命令来在本地运行项目,我们为什么不可以呢?我们需要安装webpack-dev-server依赖,执行如下命令安装:

npm install webpack-dev-server --save-dev

安装完成,让我们继续在package.json中添加这样一行代码,如下所示:

{

"name": "timeline-project",

"version": "1.0.0",

"description": "a component with vue.js",

"main": "index.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1",

"build": "webpack",

"dev": "webpack-dev-server --open --hot --port 8081" //这里是添加的代码

},

"keywords": [

"timeline"

],

"author": "",

"license": "ISC",

"devDependencies": {

"@babel/cli": "^7.11.5",

"@babel/core": "^7.11.5",

"@babel/preset-env": "^7.11.5",

"babel-loader": "^8.1.0",

"vue": "^2.6.12",

"vue-loader": "^15.9.3",

"vue-template-compiler": "^2.6.12",

"webpack": "^4.44.1",

"webpack-cli": "^3.3.12"

}

}

添加的代码很好理解,就是启动服务热更新,并且端口是8081。接下来让我们尝试运行npm run dev,看看发生了什么!

真的很棒,我们已经成功运行了vue-cli项目,搭建环境这一步到目前为止也算是成功了。

前面还提到一个问题,那就是在webpack.config.js中为什么要加上resolve配置,这是因为,如果我们需要在.vue文件中使用components选项来注册一个组件的话,就必须要引入完整的vue.js,也就是编译模板代码,如果我们只用render来创建一个组件,那么就不需要添加这个配置,这就是官网所说的运行时 + 编译器 vs. 只包含运行时。

在这里我们还要注意一个问题,那就是我们需要处理单文件组件中的css样式,所以我们需要安装css-loader与style-loader依赖。执行如下命令:

npm install style-loader css-loader --save-dev

在webpack.config.js中添加如下代码:

const VueLoaderPlugin = require('vue-loader/lib/plugin');

module.exports = {

mode:"development",

entry:'./main.js',

output:{

path:__dirname,

filename:'build.js'

},

resolve: {

// if you don't write this,you can't use components to register component

//only use render to register component

alias: {

'vue': 'vue/dist/vue.js'

}

},

module:{

rules:[

{

test: /\.vue$/,

loader: "vue-loader"

},

{

test: /\.css$/,

loader: ["style-loader","css-loader"]

},

{

test: /\.js$/,

loader: "babel-loader",

exclude: /node_modules/

}

]

},

plugins: [

new VueLoaderPlugin()

]

}

如果是用less或者stylus或者scss,我们还需要格外安装依赖,例如less需要安装less-loader,这里我们就只用style-loader与css-loader即可,如对less等感兴趣可自行研究。

二.分析时间线组件结构以及搭建基本架构

时间线组件可以分成三部分组成,第一部分即时间线,第二部分即时间戳,第三部分则是内容。我们先来看时间线组件的一个结构:

从上图我们可以看到时间线组件包含两个组件,即timeline与timeline-item组件,接下来我们来分析一下组件的属性有哪些。首先是父组件timeline组件,根据element ui官方文档。我们可以看到父组件仅仅只提供了一个reverse属性,即指定节点排序方向,默认为正序,但实际上我们还可以添加一个属性,那就是direction属性,因为element ui默认给的时间线组件只有垂直方向,而并没有水平方向,因此我们提供这个属性来确保时间线组件分为水平时间线和垂直时间线。

根据以上分析,我们总结如下:

direction:'vertical' //或'horizontal'

reverse:true //或false

接下来,我们来看子组件的属性,它包含时间戳,是否显示时间戳,时间戳位置,节点的类型,节点的图标,节点的颜色以及节点的尺寸。这里我们暂时忽略图标这个选项。因此我们可以将属性定义如下:

timestamp:'2020/9/1' //时间戳内容

showTimestamp:true //或false,表示是否显示时间戳

timestampPlacement:'top' //或'bottom',即时间戳的显示位置

nodeColor:'#fff'//节点的颜色值

nodeSize:'size' //节点的尺寸

nodeIcon:'el-icon-more' //节点的图标,在这里我们没有引入element ui组件,因此不添加这个属性,如果要添加这个属性,需要先编写图标组件

确定了以上属性之后,我们就可以先来编写一个静态的组件元素结构,如下图所示:

根据以上代码,我们可以清晰的看到一个时间线的元素构成,为了确保布局方便,我们多写几个子元素,即time-line-item以及它的所有子元素。接下来,我们开始编写静态的样式。如下:

.timeline {

font-size: 14px;

margin: 0;

background-color: #ffffff;

}

.timeline-item {

position: relative;

padding-bottom: 20px;

}

.timeline-item-tail {

position: absolute;

}

.is-vertical .timeline-item-tail {

border-left: 3px solid #bdbbbb;

height: 100%;

left: 3px;

}

.is-horizontal .timeline-item .timeline-item-tail {

width: 100%;

border-top: 3px solid #bdbbbb;

top: 5px;

}

.is-horizontal:after {

content: " ";

display: block;

height: 0;

visibility: hidden;

clear: both;

}

.timeline-item.timeline-item-info .timeline-item-tail {

border-color: #44444f;

}

.timeline-item.timeline-item-info .timeline-item-node {

background-color: #44444f;

}

.timeline-item.timeline-item-info .timeline-item-content {

color: #44444f;

}

.timeline-item.timeline-item-primary .timeline-item-tail {

border-color: #2396ef;

}

.timeline-item.timeline-item-primary .timeline-item-node {

background-color: #2396ef;

}

.timeline-item.timeline-item-primary .timeline-item-content {

color: #2396ef;

}

.timeline-item.timeline-item-success .timeline-item-tail {

border-color: #23ef3e;

}

.timeline-item.timeline-item-success .timeline-item-node {

background-color: #23ef3e;

}

.timeline-item.timeline-item-success .timeline-item-content {

color: #23ef3e;

}

.timeline-item.timeline-item-warning .timeline-item-tail {

border-color: #efae23;

}

.timeline-item.timeline-item-warning .timeline-item-node {

background-color: #efae23;

}

.timeline-item.timeline-item-warning .timeline-item-content {

color: #efae23;

}

.timeline-item.timeline-item-error .timeline-item-tail {

border-color: #ef5223;

}

.timeline-item.timeline-item-error .timeline-item-node {

background-color: #ef5223;

}

.timeline-item.timeline-item-error .timeline-item-content {

color: #ef5223;

}

.is-horizontal .timeline-item {

float: left;

}

.is-horizontal .timeline-item-wrapper {

padding-top: 18px;

left: -28px;

}

.timeline-item-node {

background-color: #e1e6e6;

border-radius: 50%;

display: flex;

align-items: center;

justify-content: center;

position: absolute;

}

.timeline-item-node-normal {

width: 12px;

height: 12px;

left: -2px;

}

.timeline-item-node-large {

width: 14px;

height: 14px;

left: -4px;

}

.timeline-item-wrapper {

position: relative;

top: -3px;

padding-left: 28px;

}

.timeline-item-content {

font-size: 12px;

color: #dddde0;

line-height: 1;

}

.timeline-item-timestamp {

color: #666;

}

.timeline-item-timestamp.is-top {

margin-bottom: 8px;

padding-top: 6px;

}

.timeline-item-timestamp.is-bottom {

margin-top: 8px;

}

.timeline-item:last-child .timeline-item-tail {

display: none;

}

接下来我们就要开始实现组件的逻辑封装了,首先我们需要封装timeline组件,为了将该组件归纳到一个目录下,我们先新建一个目录,叫timeline,然后新建一个index.vue文件,并且将我们编写好的css代码给移到该文件下,现在,你看到该文件的代码应该如下所示:

export default {

name: "timeline"

}

.timeline {

font-size: 14px;

margin: 0;

background-color: #ffffff;

}

.timeline-item {

position: relative;

padding-bottom: 20px;

}

.timeline-item-tail {

position: absolute;

}

.is-vertical .timeline-item-tail {

border-left: 3px solid #bdbbbb;

height: 100%;

left: 3px;

}

.is-horizontal .timeline-item .timeline-item-tail {

width: 100%;

border-top: 3px solid #bdbbbb;

top: 5px;

}

.is-horizontal:after {

content: " ";

display: block;

height: 0;

visibility: hidden;

clear: both;

}

.timeline-item.timeline-item-info .timeline-item-tail {

border-color: #44444f;

}

.timeline-item.timeline-item-info .timeline-item-node {

background-color: #44444f;

}

.timeline-item.timeline-item-info .timeline-item-content {

color: #44444f;

}

.timeline-item.timeline-item-primary .timeline-item-tail {

border-color: #2396ef;

}

.timeline-item.timeline-item-primary .timeline-item-node {

background-color: #2396ef;

}

.timeline-item.timeline-item-primary .timeline-item-content {

color: #2396ef;

}

.timeline-item.timeline-item-success .timeline-item-tail {

border-color: #23ef3e;

}

.timeline-item.timeline-item-success .timeline-item-node {

background-color: #23ef3e;

}

.timeline-item.timeline-item-success .timeline-item-content {

color: #23ef3e;

}

.timeline-item.timeline-item-warning .timeline-item-tail {

border-color: #efae23;

}

.timeline-item.timeline-item-warning .timeline-item-node {

background-color: #efae23;

}

.timeline-item.timeline-item-warning .timeline-item-content {

color: #efae23;

}

.timeline-item.timeline-item-error .timeline-item-tail {

border-color: #ef5223;

}

.timeline-item.timeline-item-error .timeline-item-node {

background-color: #ef5223;

}

.timeline-item.timeline-item-error .timeline-item-content {

color: #ef5223;

}

.is-horizontal .timeline-item {

float: left;

}

.is-horizontal .timeline-item-wrapper {

padding-top: 18px;

left: -28px;

}

.timeline-item-node {

background-color: #e1e6e6;

border-radius: 50%;

display: flex;

align-items: center;

justify-content: center;

position: absolute;

}

.timeline-item-node-normal {

width: 12px;

height: 12px;

left: -2px;

}

.timeline-item-node-large {

width: 14px;

height: 14px;

left: -4px;

}

.timeline-item-wrapper {

position: relative;

top: -3px;

padding-left: 28px;

}

.timeline-item-content {

font-size: 12px;

color: #dddde0;

line-height: 1;

}

.timeline-item-timestamp {

color: #666;

}

.timeline-item-timestamp.is-top {

margin-bottom: 8px;

padding-top: 6px;

}

.timeline-item-timestamp.is-bottom {

margin-top: 8px;

}

.timeline-item:last-child .timeline-item-tail {

display: none;

}

三.时间线组件逻辑

为了确保父子组件共享状态,我们利用provide/inject API来传递this对象,如下所示:

export default {

name: "timeline",

provide(){

return {

timeline:this

}

}

}

然后我们开始定义父组件的属性,根据前面所述,它包含两个属性,因此我们定义好在props中,如下所示:

import { oneOf } from '../util'

export default {

name: "timeline",

provide(){

return {

timeline:this

}

},

props:{

reverse:{

type:Boolean,

default:false

},

direction:{

type:String,

default:'vertical',

validator:(value) => {

return oneOf(['vertical','horizontal'],value,'vertical');

}

}

}

}

上面代码需要用到一个工具函数oneOf,顾名思义,就是必须是其中的一项,它有三个参数,第一个参数是匹配的数组,第二个参数是匹配的项,第三个是提供的默认项,该工具函数代码如下:

export const oneOf = (arr,value,defaultValue) => {

return arr.reduce((r,i) => i === value ? i : r,defaultValue);

}

其实也不难理解,就是我们想要的值必须是数组的一项,如果不是就返回默认项,工具函数内部代码,我们可以写得更清晰明了一点,如下所示:

export const oneOf = (arr,value,defaultValue) => {

return arr.reduce((result,item) => {

return item === value ? item : value;

},defaultValue);

}

以上的代码经过简洁处理就得到了前面的一行代码,如果理解不了,可以采用后者的代码,至于validator验证选项,可参考vue-prop-validator-自定义验证函数。

接下来,我们在render方法中去渲染这个父组件,代码如下:

import { oneOf } from '../util'

export default {

name: "timeline",

provide(){

return {

timeline:this

}

},

props:{

reverse:{

type:Boolean,

default:false

},

direction:{

type:String,

default:'vertical',

validator:(value) => {

return oneOf(['vertical','horizontal'],value,'vertical');

}

}

},

//新添加的内容

render(){

const reverse = this.reverse;

const direction = this.direction;

const classes = {

'timeline':true,

'is-reverse':reverse,

['is' + direction]:true

}

const slots = this.$slots.default || [];

if(reverse)slots = slots.reverse();

return (

{slots}
)

}

}

以上代码似乎不是很好理解,其实也不难理解,首先是获取reverse和direction属性,接着就是设置类名对象,类型对象包含三个,然后就是获取该组件的默认插槽内容,判断如果提供了reverse属性,则调用数组的reverse方法来让slots倒序,在这里我们应该很清晰的明白 slots如果存在,那么则一定是一个vNode节点组成的数组,如果没有,默认就是一个空数组。然后最后返回一个父元素包含该插槽的jsx元素。

在这里,我们使用了jsx语法,而我们的项目环境当中还并没有添加处理jsx的依赖,所以我们需要再次安装处理jsx语法的依赖babel-plugin-syntax-jsx babel-plugin-transform-vue-jsx,并添加相应的配置。继续在终端输入以下命令安装依赖:

npm install babel-plugin-syntax-jsx babel-plugin-transform-vue-jsx --save-dev

然后在babel.config.json中添加一行配置代码如下:

"plugins": ["transform-vue-jsx"]

接着在webpack.config.js中添加一行配置处理如下:

{

test: /\.(jsx?|babel|es6|js)$/,

loader: 'babel-loader',

exclude: /node_modules/

}

现在,我们可以看到页面中不会报错,未处理jsx了。

到此为止,父组件的逻辑代码也就完成了,接下来,让我们在全局里面使用一下该组件,看看是否生效。

在main.js里面添加如下一行代码:

import Timeline from './components/timeline/timeline.vue'

Vue.component(Timeline.name,Timeline)

然后在App.vue里面,我们使用这个组件,代码如下:

ok,似乎看起来没什么问题,让我们继续编写子组件的逻辑代码。

接下来,新建一个item.vue文件,在该文件中编写如下代码:

:class="[`timeline-item-node-${ size || ''}`,`timeline-item-node-${type || ''}`]"

:style="{ backgroundColor:color }"

v-if="!$slots.dot"

>

:class="[`is-`+ timestampPlacement ]"

v-if="item.placement === 'top' && showTimestamp"

>{{ timestamp }}

:class="[`is-`+ timestampPlacement ]"

v-if="item.placement === 'bottom' && showTimestamp"

>{{ timestamp }}

export default {

name:"timeline-item",

inject:['timeline'],

props:{

timestamp:String,

showTimestamp:{

type:Boolean,

default:false

},

timestampPlacement:{

type:String,

default:'top',

validator:(value) => {

return oneOf(['top','bottom'],value,'bottom')

}

},

type:{

type:String,

default:'default',

validator:(value) => {

return oneOf(['default','info','primary','success','warning','error'],value,'default');

}

},

size:{

type:String,

default:'normal',

validator:(value) => {

return oneOf(['normal','large'],value,'normal')

}

},

color:String

}

}

编写完成之后,让我们继续在main.js中引用它,然后在App.vue中使用它。代码分别如下:

//main.js

import TimelineItem from './components/timeline/timeline-item.vue'

Vue.component(TimelineItem.name,TimelineItem)

待审核

审核中

审核失败

审核完成

接下来我们就可以看到一个时间线已经完美的展示了,时间线组件算是大功告成了。

本文档已经录制为视频,地址如下:

搭建开发环境。

分析与创建时间线基本架构。

编写时间线逻辑。

源码和文档已经上传到github。

<think>我们将在单个组件中实现一个简单的布局,包含顶部导航栏和主页内容区域。为了简化,我们不使用路由,而是通过条件渲染来切换内容(虽然在实际项目中通常使用路由,但根据问题要求,我们在单个组件内完成)。设计思路:1.顶部导航栏:包含系统标题和导航菜单(例如:首页、关于)2.内容区域:根据当前选中的导航项显示不同的内容步骤:1.创建Vue3组件,使用CompositionAPI。2.定义状态currentPage,用于表示当前显示的是哪个页面(例如:&#39;home&#39;,&#39;about&#39;)。3.在导航栏的菜单项上绑定点击事件,切换currentPage的值。4.在内容区域根据currentPage的值显示不同的内容。注意:为了简化,我们不会创建额外的子组件,而是将全部内容写在一个组件中。示例代码如下:</think>### 在 Vue3 单个组件中创建带有导航和主页的简单页面 以下是一个在单个 `.vue` 文件中实现包含导航栏和主页内容的完整解决方案: ```vue <template> <div class="app-container"> <!-- 顶部导航栏 --> <header class="header"> <div class="logo">系统标题</div> <nav class="nav"> <button :class="{ active: currentPage === &#39;home&#39; }" @click="currentPage = &#39;home&#39;" > 首页 </button> <button :class="{ active: currentPage === &#39;page1&#39; }" @click="currentPage = &#39;page1&#39;" > 页面1 </button> <button :class="{ active: currentPage === &#39;page2&#39; }" @click="currentPage = &#39;page2&#39;" > 页面2 </button> </nav> </header> <!-- 主要内容区域 --> <main class="main-content"> <div v-if="currentPage === &#39;home&#39;" class="home-page"> <h1>欢迎来到主页</h1> <p>这是主页的内容区域,可以展示系统概览信息。</p> <div class="dashboard"> <div class="card"> <h3>统计卡片1</h3> <p>重要数据展示</p> </div> <div class="card"> <h3>统计卡片2</h3> <p>其他信息展示</p> </div> </div> </div> <div v-if="currentPage === &#39;page1&#39;" class="page"> <h2>页面1内容</h2> <p>这是第一个页面的详细内容区域。</p> </div> <div v-if="currentPage === &#39;page2&#39;" class="page"> <h2>页面2内容</h2> <p>这是第二个页面的详细内容区域。</p> </div> </main> <!-- 页脚 --> <footer class="footer"> © 2023 系统名称 - 页脚信息 </footer> </div> </template> <script setup> import { ref } from &#39;vue&#39;; // 当前显示的页面 const currentPage = ref(&#39;home&#39;); </script> <style> /* 全局样式 */ * { margin: 0; padding: 0; box-sizing: border-box; } .app-container { display: flex; flex-direction: column; min-height: 100vh; font-family: &#39;Arial&#39;, sans-serif; } /* 头部样式 */ .header { display: flex; justify-content: space-between; align-items: center; padding: 0 20px; height: 60px; background-color: #2c3e50; color: white; } .logo { font-size: 1.5rem; font-weight: bold; } .nav button { margin-left: 15px; padding: 8px 15px; background: none; border: none; color: #ecf0f1; cursor: pointer; border-radius: 4px; transition: background-color 0.3s; } .nav button:hover { background-color: rgba(255, 255, 255, 0.1); } .nav button.active { background-color: #42b983; color: white; } /* 主要内容区域 */ .main-content { flex: 1; padding: 20px; background-color: #f5f7fa; } .home-page { max-width: 1200px; margin: 0 auto; } .home-page h1 { margin-bottom: 20px; color: #2c3e50; } .dashboard { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; margin-top: 30px; } .card { padding: 20px; background: white; border-radius: 8px; box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1); } /* 页面内容样式 */ .page { max-width: 1200px; margin: 0 auto; padding: 20px; background: white; border-radius: 8px; box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1); } /* 页脚样式 */ .footer { padding: 15px; text-align: center; background-color: #2c3e50; color: white; } </style> ``` ### 实现说明 1. **导航功能**: - 使用 `currentPage` 响应式变量跟踪当前页面 - 导航按钮通过 `:class` 绑定动态样式,高亮显示当前页面 - 点击按钮时更新 `currentPage` 值切换内容 2. **页面结构**: - **顶部导航栏**:包含系统标题和导航按钮 - **主要内容区域**:根据当前页面显示不同内容 - **页脚**:显示版权信息等 3. **样式设计**: - 使用 Flexbox 实现响应式布局 - 卡片式设计增强视觉层次感 - 交互效果(按钮悬停、激活状态) 4. **内容区域**: - 主页展示欢迎信息和统计卡片 - 其他页面显示特定内容 - 使用 `v-if` 指令进行条件渲染 ### 使用说明 1. 创建 `App.vue` 文件并粘贴上述代码 2. 根据需要修改以下部分: - 系统标题(`<div class="logo">`) - 导航按钮文本和数量 - 各页面显示内容 - 颜色方案(修改 CSS 变量) ### 扩展建议 1. **添加路由功能**: ```javascript import { useRouter } from &#39;vue-router&#39; const router = useRouter() // 导航按钮改为 <button @click="router.push(&#39;/home&#39;)">首页</button> ``` 2. **响应式优化**: ```css @media (max-width: 768px) { .nav { display: flex; flex-direction: column; } } ``` 3. **组件拆分**(当项目复杂时): - 将导航栏拆分为 `Navbar.vue` - 将页脚拆分为 `Footer.vue` - 各页面内容拆分为独立组件
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值