初始化vue項目
npm install -g vue-clivue init webpack demo # 中間會讓你選npm yarn 等來安裝依賴,我選的是yarn,因為它快些
安裝mint-ui
yarn add mint-ui
mint-ui裝好了,還要配置一下babel,方法跟著mint-ui的官方文檔來配置就可以了
下面是我配置好的 .babelrc 文件,啟動的時候會報跟es2015相關的錯,裝一下 babel-preset-es2015 就好了
{ "presets": [ ["env", { "modules": false, "targets": { "browsers": ["> 1%", "last 2 versions", "not ie <= 8"] } }], "stage-2", ["es2015", { "modules": false }] ], "plugins": [["component", [ { "libraryName": "mint-ui", "style": true } ]],"transform-vue-jsx", "transform-runtime"], "env": { "test": { "presets": ["env", "stage-2", "es2015"], "plugins": ["transform-vue-jsx", "transform-es2015-modules-commonjs", "dynamic-import-node"] } }}
集成
打開創建的vue項目demo,在src里找到 components/HelloWorld.vue 文件,然后將內容換成下面內容
<template> <div> <mt-swipe :auto="2000"> <mt-swipe-item v-for="item in items" :key="item.id"> <a :href="item.href" rel="external nofollow" > <img :src="item.url" class="img"/> <span class="desc"></span> </a> </mt-swipe-item> </mt-swipe> </div></template><script> import {Swipe, SwipeItem} from 'mint-ui' import 'mint-ui/lib/style.css' export default { components: { 'mt-swipe': Swipe, 'mt-swipe-item': SwipeItem }, data () { return { items: [{ title: '你的名字', href: 'http://google.com', url: 'http://localhost:8080/static/img1.png' }, { title: '我的名字', href: 'http://baidu.com', url: 'http://localhost:8080/static/img2.png' }] } } }</script><style scoped> img { width: 100%; } .mint-swipe { height: 218px; } .desc { font-weight: 600; opacity: .9; padding: 5px; height: 20px; line-height: 20px; width: 100%; color: #fff; background-color: gray; position: absolute; bottom: 0; }</style>
找兩張圖片,名字分別是 img1.png , img2.png , 放在demo項目的static里,然后啟動項目
npm run dev
打開瀏覽器:http://localhost:8080/
注意
1.如果發現文字都是居中的
可以找到文件 App.vue 把里面的居中css代碼去掉就好了
1.如果頁面有內邊距
設置一下 body 的樣式 margin: 0 auto;
1.頁面里用的時候,必須要給類樣式一個高度,要不然圖片不出來 .mint-swipe { height: 218px; }
總結
以上所述是小編給大家介紹的vue.js整合mint-ui里的輪播圖實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答