Home >Web Front-end >JS Tutorial >How to choose components and framework structure of vue
This time I will show you how to choose the components and framework structure of vue. What are the precautions when choosing vue components and framework structure? The following is a practical case, let's take a look.
vue project infrastructure
For a vue project, I think the smallest subset is actually {vue, vue-router, component}. As a basic library, vue provides us with two-way binding and other functions. vue-router connects different "pages", and component is output as style or behavior. You can use these three things to implement the most basic static SPA website. Of course, I will not talk about the broad concept of vue family bucket here. I will list the main technical points one by one.
1.vue-cli: Build the basic vue project skeleton, scaffolding tools
2.sass-loader&node-sass: I use sass as a style pre-compilation tool. Both are indispensable. You can also choose by yourself, either less or stylus
3.postcss: The key to realizing responsive layout, px=>rem. Dado has proposed a layout plan based on vw and vh, but I am taking a wait-and-see attitude for the time being.
4.vuex: Manage complex data flow, state machine tools, specialized Flux
5.vuex-persistedstate: Tool for persisting state in vuex
6.vue-router: Implement jump between "pages" between SPA
7.vue-lazyload: Implement lazy loading of images and optimize http transmission performance
8.vue-awesome-swiper: Implementation of carousel function and completion of some special switching effects
9.better-scroll: Implement list scrolling and scrolling issues between parent and child components
10.axios: http tool to request data from API and implement interceptor
11.fastclick: Library to solve 300ms delay
The above are all what I think a medium-to-large Vue project needs to use. There are also some, such as the jsx syntax I used to implement image uploading, which requires something like babel-jsx. If it is not universal, it will not work. Example.
Let’s briefly describe the things mentioned above. Some things will be discussed in detail separately:
1.vue-cli:
https://github.com/vuejs/vue-cli
Scaffolding tools, when we choose vue as our development technology stack, we need to start building a directory and development environment for our project. After installing node, install it through subsequent commands
npm install -g vue-cli Install vue-cli into the global environment
vue init webpack my-vue-demo creates a vue project named my-vue-demo file name based on the webpack template
There are 6 templates here, but the one we commonly use is webpack.
During this period, you will see some unit testing tools such as e2e and ESLint tools for checking code quality. I think there is no need to install them.
The picture above is a Vue skeleton that is relatively mature in terms of projects, except the basic structure of vue-cli.
2&3 :sass,postcss
The age of writing CSS directly has passed. Precompiled style processors help us liberate productivity and improve efficiency. Sass, less, and stylus each have their own advantages and disadvantages, and each has its followers.
To use sass, you need to install sass-loader and node-sass. However, node-sass is not easy to install and is blocked. It is recommended to use Taobao's image. If the error still cannot be parsed after the installation is completed, you may need to go to webpack.base.conf.js to see if the corresponding loader has been set up.
Common functions of postcss
px2rem => can help us convert px to rem units. You only need to define the corresponding conversion standards.
autoprefixer => Postcss can also help us handle compatibility.
//vue-loader.conf.js module.exports = { loaders: utils.cssLoaders({ sourceMap: isProduction ? config.build.productionSourceMap : config.dev.cssSourceMap, extract: isProduction }), postcss: [ require('autoprefixer')({ browsers: ['iOS >= 7', 'Android >= 4.1'] }), require('postcss-px2rem') ({ remUnit: 64 }) ] }
4,5:vuex,vuex-persistedstate
https://github.com/robinvdvleuten/vuex-persistedstate
A medium to large Vue project will definitely have complex states that need to be managed. The simple event bus is no longer suitable.
特化的Flux架构,vuex就迎头顶上。简而言之:他就是我们处理无论是用户操作,API返回,URL变更等多重操作的状态管理工具。以后我会具体的说下vuex。
用过vuex的人,会发现一个很痛苦的地方,就是vuex里面的state,只要我们去刷新,它就被释放掉了。有的状态还好,没有了大不了我们可以让用户去重新操作一遍。但是像登录这样的操作,你不可能让用户刷一下就去登一下吧。当然,你会说,我们可以存到local啊,cookie里面啊。是可以!但是这样的话,state就和local里的数据形成了一种松散的关联,state显得十分的脆弱,因为你无法预知你什么时候会少写一个setStore这样的方法。vuex-persistedstate帮我们解决了这样的问题,它帮我们直接把state映射到了本地的缓存环境,我们可以在computed里面用vuex提供的mapState辅助函数,来动态的更新local里面的数据。而不需要持久化的state,我们依旧可以刷新来释放掉。
6.vue-router
当我们使用vue来构建SPA的应用时,就等于说我们完全的分离了前后端。或者通俗点的说:这就是一个纯前端的项目。后端仅仅提供数据,任何的逻辑都在前端实现。既然"脱离"了后端,那么肯定就没有request Mapping这样的同步映射url了。那么,前端就需要router来实现我们前端"页面"的跳转。vue-router就帮我们做了这样的事情,他提供给了路由守卫给我们,我们可以设置全局的,组件内的路由守卫,来实现特定的业务逻辑。 提供过渡动画,来更加生动的展示SPA应用应有的风采等等,这个以后也要具体的来说。
7.vue-lazyload
https://github.com/hilongjw/vue-lazyload
实现图片的懒加载。这是前端性能优化的一个必须面对的问题:图片。懒加载可以减少请求的数量,而且在很直观的视觉上,也有一个良好的过渡。当然,图片我们也是需要去做一些处理的,使用webp格式来减小图片的质量,或者通过oss来对图片作处理。
8.vue-awesome-swiper
https://github.com/surmon-china/vue-awesome-swiper
通过它可以实现基本轮播,横轴的切换,横轴的列表滚动等。
例如我要去实现四个tab切换这样的功能,但是简单的display这样的效果我又觉得不是很满意。那么我们就可以通过swiper来实现,每次tab里面的content都会对应swiper的一个swiper-item。切换的tab,其实就是swiper里面的next page或者before page.
data(){ return{ swiperOption: { slidesPerView :'auto', direction: 'horizontal', freeMode : true, loop: false, spaceBetween: 20, }, } }
<swiper :options="swiperOption" ref="swiper" v-if="list&& list.length !== 0"> <swiper-slide v-for="(item,index) in list" :key="index" class="hot-item"> <router-link :to="{name:'quickCar',params:{carID:item.CarID}}" class="description_car"> <img v-lazy="item.Attachments.length !==0 && item.Attachments[0].FilePath"/> <span>¥{{item.price}}/日</span> </router-link> </swiper-slide> </swiper> <p class="noData" v-else></p>
9.better-scroll
https://github.com/ustbhuangyi/better-scroll
实现纵轴列表的滚动,以及当有嵌套的路由的时候,通过better-scroll来实现的禁止父路由随着子路由的滚动的问题。
better-scroll其实也可以去实现横轴的滚动,但是为什么不使用better-scroll来处理呢?这是因为在better-scroll实现横轴滚动的时候,我们无法在better-scorll的content的内容区域里去下向拉动我们的页面。所以导致的一个Bug就是,在better-scroll横轴滚动的区域里,页面动不了了。
10.axios
基本功能就是通过axios来请求后台接口的数据。并且axios可以配合router更好的实现类似后台的拦截器的功能,例如处理token过期这样问题。因为当token过期的时候,仅仅通过vue-router的router.beforeEach来处理就有点无能为力了。这时候就需要配合后台响应返回的code来进行url的处理。
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of How to choose components and framework structure of vue. For more information, please follow other related articles on the PHP Chinese website!