01-Vue-cli 入口文件main.js分析
- main.js中
- 创建了最外层的Vue实例
- 把App.vue这个组件,当做Vue实例内部的最顶级组件并渲染出来
- 和public/index.html 中的那个id为
app
的div关联起来
简单来说,main.js会弄好一些内容,然后把这些内容替换index.html里面的那个id为app的标签
public/index.html 文件:
<!DOCTYPE html>
<html lang="en">
<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>我是index.html文件</title>
</head>
<body>
<!-- 不管是不是div都无所谓,最主要是id="app" -->
<!-- <div id="app"></div> -->
<span id="app1"></span>
</body>
</html>
App.vue:
<template>
<div>
<p>我是父组件</p>
</div>
</template>
<script>
export default {
}
</script>
<style>
p {
width: 200px;
height: 200px;
background-color: red;
}
</style>
main.js文件
// 导入vue.js
import Vue from 'vue'
// 导入主组件App.vue
import App from './App.vue'
// 生产提示.
Vue.config.productionTip = false
//创建vue实例
//默认写法
// new Vue({
// render: h => h(App),
// }).$mount('#app1')
//变形写法
//$mount('#app1')是 el:'#app1' 的另外一种写法
new Vue({
el:'#app1',
//渲染一个东西,替换#app1
//render: h => h(App),
render: (createElement)=>{
return createElement(App);
// return createElement('strong',{
// style:{
// display:'inline-block',
// width:'200px',
// height:'200px',
// backgroundColor:'green'
// }
// });
}
// main.js 入口js文件.
// 主要就是将index.html 和 App.vue建立 一个联系.
})
效果说明
02-css作用域控制 :
不加scoped是全局样式,引入它的地方都可以用,加了scoped,只管自己和自己子组件最外层
- scoped处理css,让它作用域只是相对自己当前组件,不包括自己子组件,但同时可控制子组件最外层的标签样式
- 简单理解 就是加了scoped就是只是作用于当前自己这个vue文件,不包括其它任何文件(但当前自己组件的子组件的最外层还是可以控制到),但不加就会造成在使用该组件时,它里面的样式就变成了全局样式,
App.vue(主组件):
<template>
<!--
css作用域:
1. 不管是主组件还是子组件, 在style中不加scoped写样式 都是全局的.
2. 如果在style中加了scoped写样式,那就是一个私有样式.
但是他会作用他引入的子组件最外层的那个标签.
-->
<div>
<div class="box">我是父组件</div>
<br />
<!-- 使用子组件 -->
<son></son>
</div>
</template>
<script>
// 导入子组件
import son from "./components/son";
export default {
//注册组件
components: {
son
}
};
</script>
<style scoped>
/* 这里写的不就是私有的样式吗 */
.box {
width: 200px;
height: 200px;
background-color: green;
}
</style>
<style >
/* 这里写的不就是全局的样式吗 */
</style>
son.vue(子组件):
<template>
<div class="box">
<div class="box box2">我是子组件</div>
</div>
</template>
<script>
export default {
}
</script>
<style scoped>
div {
width: 50px;
height: 50px;
background-color: blue;
}
</style>
效果说明 此时父组件里面.box类是作用于自己的div盒子并且作用于自己的子组件的最外层
子组件的div类本身是作用于最外层的,但是div权重没有.box的大,所以外层就还是.box起作用(绿色),
子组件本身除外层外的里面那层肯定就还是div得css起作用(蓝色)
03-路由
路由是什么:
路由其实就是一个指向,把路径指向相应的组件地址
路由的原理:
根据hash值的不同展示 不同的组件
one.vue(子组件)
<template>
<div>
<div>我是a页面</div>
</div>
</template>
<script>
export default {
}
</script>
<style scoped>
div {
width: 100%;
height: 300px;
background-color: red;
}
</style>
two.vue(子组件)
<template>
<div>
<div>我是b页面</div>
</div>
</template>
<script>
export default {
}
</script>
<style scoped>
div {
width: 100%;
height: 300px;
background-color: green;
}
</style>
App.vue(主组件)
<template>
<div>
<a href="#/one">点我看a页面</a>
<a href="#/two">点我看b页面</a>
<br><br>
<!-- 使用组件 -->
<one v-if="hashValue === '#/one'"></one>
<two v-else></two>
</div>
</template>
<script>
//导入子组件
import one from './components/one'
import two from './components/two'
export default {
//注册子组件
components:{
one,
two
},
data() {
return {
hashValue:""
}
},
created() {
//这个生命周期函数只会执行一次.
this.hashValue = window.location.hash;
//给window注册一个hash值改变事件, 也就是说只要hash值发生了改变, 就会触发这个事件.
window.onhashchange = ()=>{
this.hashValue = window.location.hash;
}
},
}
</script>
<style>
</style>
效果说明: 浏览器地址栏的hash值是自己手动输上去的,弄了两个a标签之后,就自动将那个浏览器地址弄成localhost:8082/#/one或者two了,
created函数只执行一次,所以必须加上那个onhashchange事件才可以点不同的a标签跳到不同的页面
点击"点我看a页面",出现红色那个我是a页面,
点击"点我看b页面",出现绿色那个我是b页面
04路由的基本使用(vue-router来实现):
1
. 安装插件(注意在项目所在目录进行安装)
npm i vue-router
2
. 导入插件
在哪里导入插件呢?vue-router是vue的一个插件,也是一个全局的控制 ,所以最终是要挂载到new Vue上的,new Vue是在main文件中执行的,所以vue-router也就得在main文件中导入:
import VueRouter from ‘vue-router’
3
. 注册插件, 在vue中,router在vue中要全局使用,是需要注册才行的, Vue.use(VueRouter)
4
. 实例化router
let router = new VueRouter({ }) //router实例化
//配制router使用规则
let router = new VueRouter({
routes:[ // 路由规则配置,我们说了,路由就是一个地址与组件的一个指向,这个规则就是配制在这里的
{
path: 地址路径, //这里配制一个路径地址
component: 组件名 //这里对应一个.vue组件 ,也就是import一个组件,在这对应上
}
]
})
5
. 把实例注入到 new Vue
new Vue({
router //这里的router就是上面所实例化的router
})
6
. 路由出口,
app.vue中写上一个标签,router-view标签
router,需要把组件渲染在哪一个位置
router-view标签将来会被匹配到的路径对应的组件替换掉
main.js文件:
import Vue from 'vue'
import App from './App.vue'
// 导入子组件
import one from './components/one.vue'
import two from './components/two.vue'
Vue.config.productionTip = false
//1.安装vue-router插件,在当前项目目录下安装npm i vue-router
//2.导入vue-router插件 import xx from ''
import VueRouter from 'vue-router';
//3.注册
Vue.use(VueRouter);
//4.实例化和配置
let router = new VueRouter({
//配置
routes:[
{
path:'/', //地址
component:one //对应的页面组件
},
{
path:'/one', //地址
component:one //对应的页面组件
},
{
path:'/two', //地址
component:two //对应的页面组件
}
]
});
//5.挂载到new Vue中
//6.路由出口
new Vue({
render: h => h(App),
router
}).$mount('#app')
App.vue(主组件)
<template>
<div>
<a href="/#/one">点我看one页面</a>
<a href="/#/two">点我看two页面</a>
<!-- 6.路由出口,路由搬运工 -->
<router-view></router-view>
</div>
</template>
<script>
export default {
}
</script>
<style>
</style>
one.vue(子组件)
<template>
<div>
<div>我是one页面</div>
</div>
</template>
<script>
export default {
}
</script>
<style scoped>
div {
width: 100%;
height: 300px;
background-color: red;
}
</style>
two.vue(子组件)
<template>
<div>
<div>我是two页面</div>
</div>
</template>
<script>
export default {
}
</script>
<style scoped>
div {
width: 100%;
height: 300px;
background-color: green;
}
</style>
效果展示: 点击不同的a标签出来不同的页面
05-仿网易路由
main.js文件:
import Vue from 'vue'
import App from './App.vue'
// 导入子组件
import findmusic from './components/findmusic'
import mymusic from './components/mymusic'
import friend from './components/friend'
Vue.config.productionTip = false
// 1.安装插件vue-router,当前项目目录下安装
// 2.导入插件
import VueRouter from 'vue-router'
// 3.全局注册
Vue.use(VueRouter)
// 4.实例化和配置
const router = new VueRouter({
routes: [
{
path: '/',
component:findmusic
},
{
path: '/findmusic',
component:findmusic
},
{
path: '/mymusic',
component:mymusic
},
{
path: '/friend',
component:friend
},
]
})
//5.挂载到newVue
new Vue({
render: h => h(App),
router
}).$mount('#app')
//6.路由出口 <router-view></router-view>
// 路由出口,告诉router,需要把组件渲染在哪一个位置
// app.vue中写上一个标签,<router-view></router-view>
// router-view标签将来会被匹配到的路径对应的组件替换掉
findmusic.vue(子组件):
<template>
<div>
<img src="../assets/findmusic.png" alt />
</div>
</template>
<script>
export default {};
</script>
<style>
img {
width: 100%;
}
</style>
friend.vue文件(子组件):
<template>
<div>
<img src="../assets/ friend.png" alt />
</div>
</template>
<script>
export default {};
</script>
<style>
img {
width: 100%;
}
</style>
mymusic.vue文件(子组件):
<template>
<div>
<img src="../assets/mymusic.png" alt />
</div>
</template>
<script>
export default {};
</script>
<style>
img {
width: 100%;
}
</style>
App.vue(主组件):
<template>
<div>
<div class="top">
<!--三种控制跳转-->
<!-- router-link的基本使用(a标签的一种vue写法): 其实本质上呢也是一个a标签. -->
<router-link to="findmusic" class="link">发现音乐</router-link>
<!-- 编程式导航 -->
<!-- <span class="link" @click="goFindMusic">发现音乐</span>-->
<span class="link" @click="goMyMusic">我的音乐</span>
<!-- <a class="link" href="/#/findmusic">发现音乐</a>
<a class="link" href="/#/mymusic">我的音乐</a> -->
<a class="link" href="/#/friend">朋友</a>
</div>
<!-- 路由出口,路由搬运工 -->
<router-view></router-view>
</div>
</template>
<script>
export default {
methods: {
goFindMusic(){
this.$router.push('/findmusic');//这里的路径就是前面路由routes所配置的路由地址.
},
goMyMusic(){
this.$router.push('/mymusic');
}
},
};
</script>
<style scoped>
* {
margin:0px;
padding:0px;
}
.top {
height: 70px;
background-color:#242424;
text-align: center;
line-height: 70px;
border-bottom: 2px solid red;
}
.top .link {
text-decoration: none;
color: #fff;
padding-left: 18px;
padding-right: 18px;
display: inline-block;
}
.top .link:hover {
background-color: #000000;
}
</style>
效果说明: 不同的路由对应的不同的组件,“发现音乐”,“我的音乐”,"“朋友”,
06-element轮播图:
element轮播图的使用方法:
-
创建vue-cli vue create 项目名
-
安装elementui
-
导入elementui
-
// 导入组件与css import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; // 注册 Vue.use(ElementUI);
-
-
使用组件
- 复制粘贴
main.js文件:
import Vue from 'vue'
import App from './App.vue'
/*
使用elementui来做轮播图
1.安装这个插件
npm i element-ui -S
2.导入这个插件
a.导入插件本身
b.导入这个插件相关的css文件
3.注册
4.使用这个插件
*/
// 1.2.安装导入组件与css
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.config.productionTip = false
// 3.注册插件
Vue.use(ElementUI);
// 4.使用这个插件 在App.vue中
new Vue({
render: h => h(App),
}).$mount('#app')
App.vue(主组件),是复制的elementui插件中的代码:
element网站: link.
<template>
<!-- 4.使用组件 -->
<el-carousel :interval="5000" arrow="always" height="600px">
<el-carousel-item v-for="(item, index) in list" :key="index">
<img :src="item.imageUrl" alt="">
</el-carousel-item>
</el-carousel>
</template>
<script>
/**
* 1.安装axios
* 2.导入axios
* 3.使用axios
*/
// 2.导入axios
import axios from "axios";
export default {
data() {
return {
list: []
};
},
//生命周期created钩子函数
created() {
axios({
url: "http://183.237.67.218:3000/banner",
params: {
xxx: Math.random() * 999
}
}).then(res => {
//console.log(res);
//把返回的图片给保存起来.
this.list = res.data.banners;
});
}
};
</script>
<style>
img{
width: 100%;
}
</style>
效果说明: 点击按钮可以左右滑动,每隔5秒自动滑动