Vue.js-webpack related configuration
note:
Sometimes it ca npm i node-sass -D
n’t be installed. At this time, you must usecnpm i node-sass -D
Use the render function to render components in normal pages
Configure the parsing of the .vue component page in webpack
-
Run
cnpm i vue -S
to install vue as a run dependency; -
Run to
cnpm i vue-loader vue-template-compiler -D
install the parsing and converting vue package as a development dependency; -
Run
cnpm i style-loader css-loader -D
the package that will parse and convert CSS and install it as a development dependency, because CSS styles will be written in the .vue file; -
In
webpack.config.js
, add the followingmodule
rules:
module: {
rules: [
{
test: /\.css$/, use: ['style-loader', 'css-loader'] },
{
test: /\.vue$/, use: 'vue-loader' }
]
}
- Create a
App.js
component page:
<template>
<!-- 注意:在 .vue 的组件中,template 中必须有且只有唯一的根元素进行包裹,一般都用 div 当作唯一的根元素 -->
<div>
<h1>这是APP组件 - {
{msg}}</h1>
<h3>我是h3</h3>
</div>
</template>
<script>
// 注意:在 .vue 的组件中,通过 script 标签来定义组件的行为,需要使用 ES6 中提供的 export default 方式,导出一个vue实例对象
export default {
data() {
return {
msg: 'OK'
}
}
}
</script>
<style scoped>
h1 {
color: red;
}
</style>
- Create
main.js
entry file:
// 导入 Vue 组件
import Vue from 'vue'
// 导入 App组件
import App from './components/App.vue'
// 创建一个 Vue 实例,使用 render 函数,渲染指定的组件
var vm = new Vue({
el: '#app',
render: c => c(App)
});
Use template objects in Vue projects built with webpack?
- In
webpack.config.js
the Addresolve
Attribute:
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
}
Summary of grammar usage in ES6
-
Use
export default
andexport
export module members; corresponding to the ES5module.exports
andexport
-
Use
import ** from **
andimport '路径'
alsoimport {a, b} from '模块标识'
import other modules -
Use arrow functions:
(a, b)=> { return a-b; }
In the vue component page, integrate the vue-router routing module
- Import the routing module:
import VueRouter from 'vue-router'
- Install the routing module:
Vue.use(VueRouter);
- Import the components that need to be displayed:
import login from './components/account/login.vue'
import register from './components/account/register.vue'
- Create a route object:
var router = new VueRouter({
routes: [
{
path: '/', redirect: '/login' },
{
path: '/login', component: login },
{
path: '/register', component: register }
]
});
- Mount the routing object to the Vue instance:
var vm = new Vue({
el: '#app',
// render: c => { return c(App) }
render(c) {
return c(App);
},
router // 将路由对象,挂载到 Vue 实例上
});
- Transform the App.vue component, in the template, add
router-link
androuter-view
:
<router-link to="/login">登录</router-link>
<router-link to="/register">注册</router-link>
<router-view></router-view>
The css scope problem in the component
Extract routing as a separate module
Use Ele.me's MintUI component
- Import all MintUI components:
import MintUI from 'mint-ui'
- Import style sheet:
import 'mint-ui/lib/style.css'
- Use MintUI in vue:
Vue.use(MintUI)
- Examples of use:
<mt-button type="primary" size="large">primary</mt-button>
Use MUI components
- Import MUI style sheet:
import '../lib/mui/css/mui.min.css'
- In
webpack.config.js
adding new loader rules:
{ test: /\.(png|jpg|gif|ttf)$/, use: 'url-loader' }
- According to the official documents and examples, try to use related components
Host the project source code in oschina
-
Click on the picture -> Modify -> SSH public key of how to generate SSH public key
-
Create your own empty storage, use
git config --global user.name "用户名"
and thegit config --global user.email ***@**.com
user's name and mailbox submitted to the global configuration -
Use
git init
local project initialization -
Use
touch README.md
andtouch .gitignore
to create project documentation and ignored files; -
Use
git add .
will host all the files to git -
Use
git commit -m "init project"
the projects submitted by local -
Use
git remote add origin 仓储地址
local projects and remote storage connections and the use of aliases origin of the most remote storage -
Use
git push -u origin master
native code to push warehousing
Basic settings of App.vue component
-
Using the navigation bar head is fixed
Mint-UI
toHeader
the component; -
The bottom of the tab use
mui
oftabbar
; -
Shopping cart icon, use
icons-extra
themui-icon-extra mui-icon-extra-cart
same time, it should depend on the font icon filemui-icons-extra.ttf
, copied tofonts
the directory! -
The tab at the bottom, transformed into
router-link
to achieve the switching of a single page; -
There are two ways to set the highlight when Tab Bar routing is activated:
- The global setting style is as follows:
.router-link-active{
color:#007aff !important;
}
- Or
new VueRouter
when, bylinkActiveClass
specified categories highlighted:
// 创建路由对象
var router = new VueRouter({
routes: [
{
path: '/', redirect: '/home' }
],
linkActiveClass: 'mui-active'
});
Switch between different component pages of the tabbar tab
-
Tabbar transformed into the
router-link
form, and to specify each connectionto
attribute; -
Import the components that need to be displayed in the entry file, and create a routing object:
// 导入需要展示的组件
import Home from './components/home/home.vue'
import Member from './components/member/member.vue'
import Shopcar from './components/shopcar/shopcar.vue'
import Search from './components/search/search.vue'
// 创建路由对象
var router = new VueRouter({
routes: [
{
path: '/', redirect: '/home' },
{
path: '/home', component: Home },
{
path: '/member', component: Member },
{
path: '/shopcar', component: Shopcar },
{
path: '/search', component: Search }
],
linkActiveClass: 'mui-active'
});
Use mt-swipe carousel diagram component
- Fake data:
lunbo: [
'http://www.itcast.cn/images/slidead/BEIJING/2017440109442800.jpg',
'http://www.itcast.cn/images/slidead/BEIJING/2017511009514700.jpg',
'http://www.itcast.cn/images/slidead/BEIJING/2017421414422600.jpg'
]
- Introduce the carousel diagram component:
<!-- Mint-UI 轮播图组件 -->
<div class="home-swipe">
<mt-swipe :auto="4000">
<mt-swipe-item v-for="(item, i) in lunbo" :key="i">
<img :src="item" alt="">
</mt-swipe-item>
</mt-swipe>
</div>
</div>
Get data .vue
used in componentsvue-resource
-
Run the
cnpm i vue-resource -S
installation module -
Import the vue-resource component
import VueResource from 'vue-resource'
- Use vue-resource component in vue
Vue.use(VueResource);