Vue3项目练习详细步骤(第二部分:主页面搭建)

主页面搭建

页面主体结构 

路由 

子路由

 

主页面搭建

页面主体结构 

在vuews目录下新建Layout.vue文件

 主页面内容主体代码

<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
</script>

<template>
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
            <el-menu active-text-color="#ffd04b" background-color="#232323"  text-color="#fff"
                router>
                <el-menu-item >
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>文章分类</span>
                </el-menu-item>
                <el-menu-item >
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>文章管理</span>
                </el-menu-item>
                <el-sub-menu >
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>个人中心</span>
                    </template>
                    <el-menu-item >
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>基本资料</span>
                    </el-menu-item>
                    <el-menu-item >
                        <el-icon>
                            <Crop />
                        </el-icon>
                        <span>更换头像</span>
                    </el-menu-item>
                    <el-menu-item >
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>重置密码</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>用户:<strong>acc</strong></div>
                <el-dropdown placement="bottom-end">
                    <span class="el-dropdown__box">
                        <el-avatar :src="avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="profile" :icon="User">基本资料</el-dropdown-item>
                            <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item>
                            <el-dropdown-item command="password" :icon="EditPen">重置密码</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <div style="width: 1290px; height: 570px;border: 1px solid red;">
                    内容展示区
                </div>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>大事件 ©2024</el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-aside {
        background-color: #232323;

        &__logo {
            height: 120px;
            background: url('@/assets/logo.png') no-repeat center / 120px auto;
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }
}
</style>

在App.vue中引入主页面文件

但是发现都在一个页面里面 

路由 

  • 路由,决定从起点到终点的路径的进程
  • 在前端工程中,路由指的是根据不同的访问路径,展示不同组件的内容
  • Vue Router是Vue.js的官方路由 

在项目目录下安装路由

npm install vue-router@4

在src目录下创建 router,再在router目录下创建router.js文件

创建路由器,并导出  

import {createRouter,createWebHistory} from 'vue-router';

//导入组件
import LoginVue from '@/views/Login.vue'
import LayoutVue from './views/Layout.vue';

//定义路由关系
const routes = [
    {path:'/login',component:LoginVue},
    {path:'/',component:LayoutVue}
]

//创建路由器
const router = createRouter({
    history:createWebHistory(),
    routes:routes
})

//导出路由
export default router

 在main.js文件中应用实例中使用vue-router

修改App.vue文件声明router-view标签,展示组件内容

<template>
  <router-view/>
</template>

<script>

export default {
  name: 'App',
  components: {
  }
}
</script>

<style>

</style>

 浏览器访问查看路由

在登录页面Login.vue文件中设置登录成功时从登录页跳转切换到主页面

 保存查看效果

子路由

在views目录下创建五个组件

 ArticleCategory.vue

<template>
    文章分类
</template>

ArticleManage.vue

<template>
    文章管理
</template>

 UserAvatar.vue

<template>
    更换头像
</template>

UserInfo.vue 

<template>
    基本资料
</template>

 UserResetPassword.vue

<template>
    重置密码
</template>

在router.js文件中配置子路由

//导入子路由组件
import ArticleCategoryVue from '@/views/ArticleCategory.vue'
import ArticleManageVue from '@/views/ArticleManage.vue'
import UserAvatarVue from '@/views/UserAvatar.vue'
import UserInfoVue from '@/views/UserInfo.vue'
import UserResetPasswordVue from '@/views/UserResetPassword.vue'

//定义路由关系
const routes = [
    {path:'/login',component:LoginVue},
    {path:'/',component:LayoutVue,
    //重定向 为首页页面默认展示的子路由页面
    redirect: '/article/manage',
    //子路由
        children: [
            { path: '/article/category', component: ArticleCategoryVue },
            { path: '/article/manage', component: ArticleManageVue },
            { path: '/user/info', component: UserInfoVue },
            { path: '/user/avatar', component: UserAvatarVue },
            { path: '/user/password', component: UserResetPasswordVue }
        ]
    }
]

在Layout.vue文件中的中区区域代码部分 声明router-view标签

在Layout.vue主页面文件中为菜单项 el-menu-item 设置index属性(为router子路由的路径一致),设置点击后的路由路径 。

 设置完成后即可完成点击菜单子路由页面跳转

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 要搭建一个Vue2项目,需要遵循以下步骤: 1. 确认已安装Node.js Vue.js 是一个基于Node.js的框架,所以在开始之前,需要确认已经安装了Node.js。可以在终端中输入`node -v`来检查Node.js是否已经安装,如果未安装,则需要先安装Node.js。 2. 使用Vue CLI创建项目 Vue CLI是Vue.js的官方脚手架工具,可以使用它快速创建Vue项目。在终端中输入以下命令创建一个新项目: ``` npm install -g @vue/cli vue create my-project ``` 运行以上命令后,Vue CLI会询问你需要哪些特性或插件,选择完成后即可自动创建一个Vue项目。 3. 运行Vue项目 创建项目后,进入项目目录,并启动开发服务器。在终端中输入以下命令: ``` cd my-project npm run serve ``` 运行成功后,访问`http://localhost:8080`即可预览Vue项目。 以上就是搭建Vue2项目的基本步骤,根据需要,你还可以添加Vue Router、Vuex等插件来完善项目。 ### 回答2: 搭建Vue2项目的具体步骤如下: 1. 确保电脑已经安装了Node.js和npm,通过在命令行输入`node -v`和`npm -v`来检查安装情况。 2. 在命令行中输入以下命令来安装Vue脚手架工具: ``` npm install -g @vue/cli ``` 3. 创建一个新的Vue项目,在命令行输入以下命令并按照提示完成项目配置: ``` vue create 项目名称 ``` 4. 进入项目文件夹,使用以下命令启动本地开发服务器: ``` cd 项目名称 npm run serve ``` 5. 在浏览器中输入`http://localhost:8080`,你将看到Vue的欢迎页面,表示项目已经成功搭建。 6. 在项目中,你可以通过编辑`src`目录下的文件来开发你的Vue应用,要的入口文件是`main.js`,要的视图文件是`App.vue`。 7. 在开发过程中,你可以使用Vue提供的各种语法和特性,如组件、指令、计算属性等来构建你的应用。 8. 当你的应用开发完成后,你可以使用以下命令进行项目打包: ``` npm run build ``` 在项目根目录下会生成一个dist文件夹,里面包含了打包后的所有静态资源,可以将其部署到服务器上。 以上就是搭建Vue2项目的具体步骤。开始使用Vue开发之前,建议先了解Vue的基本概念和语法。祝你开发愉快! ### 回答3: 搭建Vue2项目的具体步骤如下: 1. 首先,确保你的电脑中已经安装了Node.js环境。Node.js是一种基于Chrome V8引擎的JavaScript运行环境,使用它可以方便地搭建和运行Vue项目。 2. 在命令行界面中,使用npm(Node Package Manager)命令安装Vue CLI(Command Line Interface),可以通过以下命令进行安装: ``` npm install -g @vue/cli ``` 3. 安装完成后,在命令行中输入以下命令来创建Vue项目: ``` vue create 项目名 ``` 这个命令将会从官方的Vue模板中创建一个新的项目,并自动安装项目所需的依赖。 4. 创建项目完成后,进入项目目录: ``` cd 项目名 ``` 5. 使用以下命令来启动开发服务器: ``` npm run serve ``` 这将会启动一个开发服务器,用于实时编译和热重载项目代码,并在本地开启一个预览服务。 6. 打开浏览器,输入localhost:8080(或者自动弹出的新网址)来查看运行中的项目。 7. 接下来,你可以在src目录中进行开发,包括创建.vue组件、编写Vue代码、添加样式等等。 8. 当你完成开发后,使用以下命令进行项目的构建和打包: ``` npm run build ``` 这个命令将会对项目进行优化和压缩,生成用于生产环境的静态文件。 以上就是搭建Vue2项目的具体步骤。当然,在实际项目中还可能涉及其他一些步骤,比如安装和使用其他NPM包、配置路由、管理状态等等,但以上步骤已经搭建了一个简单的Vue项目,可以用于快速开始开发。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

open_test01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值