1.安装依赖

  1. yarn add postcss-pxtorem amfe-flexible //实现适配
  2. yarn add vant

2.main.js中导入依赖

  1. import "amfe-flexible/index.js"
  2. import Vant from 'vant'
  3. import 'vant/lib/index.css'
  4. Vue.use(Vant)

3.修改postcss.config.js文件

  1. module.exports = {
  2. plugins: {
  3. 'autoprefixer': {
  4. browsers: ['Android >= 4.0', 'iOS >= 7']
  5. },
  6. 'postcss-pxtorem': {
  7. rootValue: 37.5, //如果样稿是1080px,这个地方要改
  8. propList: ['*']
  9. }
  10. }
  11. }

tip: 在public/index.html下一定要加上视口,之前注销现在要加上

  1. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

4.样式重置

  1. 找到reset.css官网 将样式复制到assets/css/reset.css
  2. import '@/assets/css/reset.css' //@是指src的根目录

5.异步路由

  1. export default new Router({
  2. mode:"hash",
  3. routes:[
  4. ...
  5. {
  6. path:"/detail",
  7. name:"detail",
  8. /* 异步路由 */
  9. component:()=>import('@/pages/Detail')
  10. }
  11. ]
  12. })

6.拆分配置路由

1.src/utils/config.js

  1. import Vue from 'vue'
  2. import 'lib-flexible/flexible.js'
  3. import '@/assets/css/reset.css'
  4. import BsBtn from '@/components/BsBtn.vue';
  5. Vue.filter("format",function(val){
  6. if(val.length>6){
  7. val=val.slice(0,6)+"..."
  8. }
  9. return val
  10. })
  11. Vue.component('BsBtn',BsBtn)
  12. export default Vue;

2.src/main.js

  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. import router from './routers'
  4. import './utils/config.js' //在main.js中导入路由页面utils/config.js
  5. Vue.config.productionTip = false
  6. new Vue({
  7. router,
  8. render: h => h(App),
  9. }).$mount('#app')

7.跨域和请求数据

1.安装依赖

  1. yarn add axios

2.config.js中导入依赖

  1. import axios from 'axios' //导入
  2. axios.defaules.baseURL='http://192.168.14.49:5000' //主地址
  3. Vue.prototype.axios=axios;

3.对应.vue中发送请求

  1. import ItemIndex from '@/components/ItemIndex' //导入组件
  2. export default {
  3. name:"Music",
  4. data(){
  5. return{
  6. playlists:[]
  7. }
  8. },
  9. //注册组件
  10. components:{
  11. ItemIndex
  12. },
  13. mounted(){
  14. this.axios.get("/top/playlist?cat=华语&limit=12").then(res=>{
  15. this.playlists=res.data.playlists
  16. })
  17. }
  18. }
  19. //4.渲染数据
  20. <template>
  21. <div class="container">
  22. <item-index :data="item" v-for="item of playlists" :key="item.id">
  23. <div slot="icon" class="icon">
  24. <img src="../../assets/images/p0.png" alt="">
  25. {{item.playCount}}
  26. </div>
  27. </item-index>
  28. </div>
  29. </template>