前端Vue项目详解

一、项目初始化

创建webpack模板项目如下所示:

MacBook-Pro:PycharmProjects hqs$ vue init webpack luffy_project ? Project name luffy_project ? Project description A Vue.js project ? Author hqs ? Vue build standalone ? Install vue-router? Yes ? Use ESLint to lint your code? No ? Set up unit tests No ? Setup e2e tests with Nightwatch? No ? Should we run `npm install` for you after the project has been created? (recommended) npm vue-cli · Generated "luffy_project".

根据提示启动项目:

$ cd luffy_project/ $ npm run dev

由于在初始化时选择了vue-router,因此会自动创建/src/router/index.js文件。

删除Helloworld组件相关信息后,index.js文件内容如下所示:

import Vue from 'vue' import Router from 'vue-router' // @绝对路径 检索到 ...src/ // 如果Router当做局部模块使用一定要Vue.use(Router) // 以后在组件中,可以通过this.$router 获取Router实例化对象 // 路由信息对象 this.$routes 获取路由配置信息 Vue.use(Router) // 配置路由规则 export default new Router({ routes: [ { 'path': 'https://www.jb51.net/' } ] })

二、基于ElementUI框架实现导航栏

1、elementUI——适合Vue的UI框架

elementUI是一个UI库,它不依赖于vue,但确是当前和vue配合做项目开发的一个比较好的UI框架。

(1)npm安装

推荐使用 npm 的方式安装,能更好地和 webpack 打包工具配合使用。

$ npm i element-ui -S

(2)CDN

目前可以通过 unpkg.com/element-ui 获取到最新版本的资源,在页面上引入 js 和 css 文件即可开始使用。

<!-- 引入样式 --> <link href="https://unpkg.com/element-ui/lib/theme-chalk/index.css" > <!-- 引入组件库 --> <script src="https://unpkg.com/element-ui/lib/index.js"></script>

使用CND引入 Element 需要在链接地址上锁定版本,以免将来 Element 升级时受到非兼容性更新的影响。锁定版本的方法请查看 unpkg.com。

2、引入 Element

在项目中可以引入整个Element,或者是根据需要仅引入部分组件。

(1)完整引入

在 main.js 中写入如下内容:

import Vue from 'vue' import App from './App' import router from './router' // elementUI导入 import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' // 注意样式文件需要单独引入 // 调用插件 Vue.use(ElementUI); Vue.config.productionTip = false; /* eslint-disable no-new */ new Vue({ el: '#app', router, components: { App }, template: '<App/>' });

以上代码便完成了 Element 的完整引入。

尝试在App.vue使用elementui的Button按钮:

<template> <div> <!-- 导航区域 --> <el-button type="info">信息按钮</el-button> <router-view/> </div> </template> <script> export default { name: 'App' } </script>

显示效果:

前端Vue项目详解

(2)按需引入

借助 babel-plugin-component,可以只引入需要的组件,以达到减小项目体积的目的。

首先安装babel-plugin-component:

$ npm install babel-plugin-component -D

然后将.babelrc文件修改如下:

{ "presets": [["es2015", { "modules": false }]], "plugins": [ [ "component", { "libraryName": "element-ui", "styleLibraryName": "theme-chalk" } ] ] }

如果只希望引入部分组件,如Buttion何Select,那么需要在 main.js 中写如下内容:

import Vue from 'vue'; import { Button, Select } from 'element-ui'; import App from './App.vue'; Vue.component(Button.name, Button); Vue.component(Select.name, Select); /* 或写为 * Vue.use(Button) * Vue.use(Select) */ new Vue({ el: '#app', render: h => h(App) });

3、导航栏实现

首先创建/src/components/Common/LuffyHeader.vue文件:

<template> <!-- element-ui --> <el-container> <el-header height = '80px' > <div> <div> <img src="https://www.luffycity.com/static/img/head-logo.a7cedf3.svg" alt=""> </div> <div> <ul> <li> <a href="#" > 导航链接 </a> </li> </ul> </div> <div> <span>登录</span> &nbsp;| &nbsp; <span>注册</span> </div> </div> </el-header> </el-container> </template> <script> export default { name: 'LuffyHeader', data(){ return { } }, }; </script>

再创建/static/global/global.css文件:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/d82b95c0249053f6ac5c408664920236.html