这篇文章内容是从vue3项目构建到实现一个简单的布局,用到组件化原理和路由,效果图:
vue3创建项目:
- vue create 项目名称
- Manually select features 选择手动选择功能
- 选择Vue version,babel,TypeScript,Router,Vuex,
- 选择3.x
- 下面一路回车等待项目创建完成
创建项目成功后vscode打开
组件化布局(.vue编写->使用页面导入->注册->使用)
- 在components文件夹下创建Header.vue,Side.vue,Footer.vue文件,内容自定
- 在App.vue里import导入组件,在components里注册组件,在template里使用
导入路由(.vue编写->router/index.ts导入->使用)
- 在views文件夹下创建需要跳转的页面
- 如何在index.ts里添加刚刚创建的页面仿照About
- 在使用的页面Header.vue里:
<router-link to="/" class="a">首页</router-link>
,
<router-link to="/articles" class="a">文章</router-link>
Header.vue
<template>
<el-container>
<div style="width: 200px;">
<img src="../assets/logo.png" alt="找不到照片" style="width:50px" />
</div>
<div style="flex: 1; text-align: center">
<el-menu
:default-active="activeIndex"
class="el-menu-demo"
mode="horizontal"
@select="handleSelect"
>
<el-menu-item index="1">
<router-link to="/" class="a">首页</router-link>
</el-menu-item>
<el-menu-item index="2">
<router-link to="/articles" class="a">文章</router-link>
</el-menu-item>
<el-menu-item index="3">
<router-link to="/archive" class="a">归档</router-link>
</el-menu-item>
<el-menu-item index="4">
<router-link to="/project" class="a">项目</router-link>
</el-menu-item>
<el-menu-item index="5">
<router-link to="/timeline" class="a">历程</router-link>
</el-menu-item>
<el-menu-item index="6">
<router-link to="/message" class="a">留言</router-link>
</el-menu-item>
<el-menu-item index="7">
<router-link to="/about" class="a">关于</router-link>
</el-menu-item>
</el-menu>
</div>
<div style="width:200px;">
<el-button type="primary">登录</el-button>
<el-button type="danger">注册</el-button>
</div>
</el-container>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const activeIndex = ref("1");
const activeIndex2 = ref("1");
const handleSelect = (key: string, keyPath: string[]) => {
console.log(key, keyPath);
};
</script>
<style scoped>
.a {
text-decoration: none;
display: block;
padding: 0;
}
</style>
Aside.vue
<template>
<div style="width: 300px;">
<div class="card" style="padding-top: 50px;">
<img
src="../assets/logo.png"
alt="找不到照片"
style="width: 80px; border-radius: 50px; border: 1px solid #eee;"
/>
<h2>HuangSina</h2>
</div>
<div class="tags">
<div class="title">标签云</div>
<!-- <router-link
v-for="item in state.list"
class="item"
:key="item._id"
:to="`/articles?tag_id=${item._id}&tag_name=${item.name}&category_id=`"
>
<span :key="item._id">{{item.name}}</span>
</router-link>-->
<el-tag class="item" type="info">Tag 3</el-tag>
<el-tag class="item" type="info">Tag 3</el-tag>
<el-tag class="item" type="info">Tag 3</el-tag>
<el-tag class="item" type="info">Tag 3</el-tag>
<el-tag class="item" type="info">Tag 3</el-tag>
</div>
<div class="card">
<h3>本站公众号</h3>
<img src="../assets/logo.png" alt="找不到照片" style="width: 100px;" />
</div>
</div>
</template>
<script lang="ts" setup>
import {
Location,
Document,
Menu as IconMenu,
Setting
} from "@element-plus/icons-vue";
const handleOpen = (key: string, keyPath: string[]) => {
console.log(key, keyPath);
};
const handleClose = (key: string, keyPath: string[]) => {
console.log(key, keyPath);
};
</script>
<style scoped>
.card {
border-bottom: 1px solid #eee;
padding: 5px;
height: 200px;
box-sizing: border-box;
}
.tags {
min-height: 200px;
padding: 5px 0 20px 0;
margin-bottom: 10px;
border-bottom: 1px solid #eee;
}
.tags .title {
font-size: 14px;
color: #969696;
}
.tags .item {
display: inline-block;
cursor: pointer;
padding: 5px 10px;
border-radius: 5px;
background-color: #eee;
color: #333;
margin: 10px 10px 0 0;
text-decoration: none;
}
.tags .item :hover {
color: #409eff;
}
</style>
Footer.vue
<template>
<div class="footer">
<div>前端修炼 ©2022 Created by HuangSina</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
name: "Footer",
});
</script>
<style scoped>
.footer {
text-align: center;
padding: 20px;
font-weight: bold;
}
</style>
App.vue
<template>
<div id="nav">
<!-- <router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link> -->
<!-- 头部 -->
<Header></Header>
<!-- 中间 -->
<el-container>
<!-- 内容 -->
<el-main>
<router-view style="felx: 1"></router-view>
</el-main>
<!-- 侧边栏 -->
<Aside></Aside>
</el-container>
<!-- 尾部 -->
<Footer></Footer>
</div>
</template>
<script>
import Header from "./components/Header"
import Aside from "./components/Aside"
import Footer from "./components/Footer"
export default {
name: 'App',
components: {
Header,
Aside,
Footer
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
}
</style>
router/index.ts
import {
createRouter,
createWebHistory,
RouteRecordRaw,
} from 'vue-router';
import Home from '../views/Home.vue';
const routes: Array<RouteRecordRaw> = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
},
{
path: '/articles',
name: 'articles',
component: () => import(/* webpackChunkName: "about" */ '../views/Articles.vue')
}
export default router
版权声明:本文为qq_55054495原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。