目录javascript
<router-view />完成页面渲染 => <router-link>(this.$router.push())
完成请求路径的切换<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- 适配移动端 疏放程度是1倍--> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <!-- 图标--> <link rel="icon" href="<%= BASE_URL %>favicon.ico"> <!-- 标题,这个能够本身进行修改--> <!-- <title>b-proj</title>--> <title>vue项目</title> </head> <body> <!--当浏览器不支持js 的时候,会打印下面的这段话--> <!--如今都支持,因此能够不须要--> <noscript> <strong>We're sorry but b-proj doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> </noscript> <!--下面的挂采点--> <div id="app"></div> <!-- built files will be auto injected --> </body> </html>
修改后css
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="icon" href="<%= BASE_URL %>favicon.ico"> <title>vue项目</title> </head> <body> <div id="app"></div> </body> </html>
style中写入的全局配置,这个能够进行删除html
<style> /*全局配置*/ #app { /*浏览器配置*/ font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; } #nav { padding: 30px; } #nav a { font-weight: bold; color: #2c3e50; } #nav a.router-link-exact-active { color: #42b983; } </style>
app.vue修改后保留前端
<script src="main.js"></script> <template> <div id="app"> <!--提供一个视图组件占位符,占位符被哪一个views下的视图组件替换,浏览器就显示哪一个页面组件--> <router-view/> </div> </template>
1.入口文件:加载vue、router、store等配置 以及 加载自定义配置(本身的js、css,第三方的js、css)
2.建立项目惟一根组件,渲染App.vue,挂载到index.html中的挂载点 => 项目页面显示的就是 App.vue 组件
3.在App.vue中设置页面组件占位符
4.浏览器带着指定 url连接 访问vue项目服务器,router组件就会根据 请求路径 匹配 映射组件,去替换App.vue中设置页面组件占位符
eg: 请求路径 /user => 要渲染的组件 User.vue => 替换App.vue中的 <router-view/>
vue
import Vue from 'vue' import App from './App.vue' import router from './router' import store from './store' Vue.config.productionTip = false; new Vue({ router, store, render: h => h(App) }).$mount('#app');
修改后java
import Vue from 'vue' // 加载vue环境 import App from './App.vue' // 导入根组件 import router from './router' // 加载路由环境 vue-router import store from './store' // 加载仓库环境 vuex Vue.config.productionTip = false; // Tip提示 new Vue({ el: '#app',//挂载点 router: router,//路由 store, render: function (read_root_vue) {//read_root_vue任意的一个函数,和h相同 return read_root_vue(App) } });
store是全局的单列webpack
存储web
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); export default new Vuex.Store({ //在store厂库的内部,通常在state内写要存入的数据 state: { //car: { //name: '默认', //price: 0 //} }, mutations: {}, actions: {}, modules: {} })
存储vue-router
//this表明的是vue对象 //this.$store.state.car = car; export default { name: "Car", props: ['car'], methods: { goDetail(car) { // 先将要显示的汽车对象存储到仓库,详情页加载后,本身去仓库中获取 // console.log(this.$store.state); this.$store.state.car = car; this.$router.push('/car/detail') } } }
取出,通常使用,建立的时候createvuex
//this.car = this.$store.state.car; export default { name: "CarDetail", data() { return { car: {} } }, created() { // console.log(this.$store.state.car); // this.car = {name: '五菱宏光', price: 120} this.car = this.$store.state.car; } }
导出:
export default { name: 'home', components: { }, data() { return { } } }
导入:
import Home from '../views/Home.vue
注册
export default { name: 'home', // 二、注册要使用的小组件 components: { Nav, Footer, Book, }, data() { return { books } } }
使用
在<template></template>
内部使用
<template> <div class="home"> <!--vue项目环境下,模板也受vue环境控制,使用标签支持大小写--> <!--三、使用导入的小组件--> <Nav></Nav> <div class="main"> <!-- key属性是为标签创建缓存的标识,不能重复,且在循环组件下,必须设置 --> <Book v-for="book in books" :book="book" :key="book.title" /> </div> <Footer></Footer> </div> </template>
<router-link :to="'/book/detail/' + book.id">{{ book.title }}</router-link>
<router-link :to="{name: 'book-detail', params: {pk: book.id}}">{{ book.title }}</router-link>
this.$router.push(`/book/detail/${id}`);
this.$router.push({ name: 'book-detail', params: {pk: id}, });
this.$router.go(-1)//向后跳转1页 this.$router.go(-2)//向后跳转2页 this.$router.go(1)//向前跳转1页 this.$router.go(2)//向前跳转2页
import Home from '../views/Home.vue'
{ path: '/', name: 'home', component: Home },
{ path: '/index', redirect: '/' },
{ path: '/user', name: 'user', component: User },
{ path: '/book/detail/:pk', name: 'book-detail', component: BookDetail },
另一种导入方式
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
等同于component: () => import( '../views/About.vue')
等同于import About from '../views/About.vue
{ 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') }
'../assets/img/西游记.jpg'
let img1 = require('../assets/img/西游记.jpg');
就是require(资源的相对路径)//import '@/assets/css/global.css' 方法1 require('@/assets/css/global.css');
import settings from '@/assets/js/settings' Vue.prototype.$settings = settings;//原生的配置,在调用的时候能够方便使用
//settings.js //导出 export default { base_url: 'http://localhost:8000', }
使用
this.$settings.base_url
当Vue用 v-for 正在更新已渲染过的元素列表是,它默认用“就地复用”策略。若是数据项的顺序被改变,Vue将不是移动DOM元素来匹配数据项的改变,而是简单复用此处每一个元素,而且确保它在特定索引下显示已被渲染过的每一个元素。
为了给Vue一个提示,以便它能跟踪每一个节点的身份,从而重用和从新排序现有元素,你须要为每项提供一个惟一 key 属性。key属性的类型只能为 string或者number类型。