vue常用插件

news/2025/2/22 6:55:50

vueawesomeswiper_0">一、轮播图-------vue-awesome-swiper

安装:npm install vue-awesome-swiper --save
vue中使用:

  • 全局使用
mport Vue from 'vue'
import VueAwesomeSwiper from 'vue-awesome-swiper'
 
// require styles
import 'swiper/dist/css/swiper.css'
 
Vue.use(VueAwesomeSwiper, /* { default global options } */)
  • 在组件中使用
import 'swiper/dist/css/swiper.css'
 
import { swiper, swiperSlide } from 'vue-awesome-swiper'
 
export default {
  components: {
    swiper,
    swiperSlide
  }
}

HTML/JS用法

<template>
  <swiper :options="swiperOption">
    <swiper-slide v-for="slide in swiperSlides">I'm Slide {{ slide }}</swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</template>
 
<script>
  export default {
    name: 'carrousel',
    data() {
      return {
        swiperOption: {
          pagination: {
            el: '.swiper-pagination'
          }
        },
        swiperSlides: [1, 2, 3, 4, 5]
      }
    },
    mounted() {
      setInterval(() => {
        console.log('simulate async data')
        if (this.swiperSlides.length < 10) {
          this.swiperSlides.push(this.swiperSlides.length + 1)
        }
      }, 3000)
    }
  }
</script>

② next.js中使用

  • 全局使用
    1、在 plugins 文件夹下新建文件 vue-awesome-swiper.js
import Vue from 'vue'
import VueAwesomeSwiper from 'vue-awesome-swiper/dist/ssr'
 
Vue.use(VueAwesomeSwiper)

2、在 nuxt.config.js 文件中配置插件

css: [
    '@/assets/css/swiper.css'
  ],
  plugins: [
   { src: "~/plugins/vue-awesome-swiper.js", ssr: false }
  ]
  • 在组件中使用
    不需要新建文件也不要配置文件,直接在组件中使用,nuxt项目中,swiper只在浏览器端引入使用,且是require形式,因为import只能写在最外层
import Vue from 'vue'

// If used in nuxt.js/ssr, you should keep it only in browser build environment
if (process.browser) {
  const VueAwesomeSwiper = require('vue-awesome-swiper/dist/ssr')
  Vue.use(VueAwesomeSwiper)
}

export default{
	data(){
		//...
	}
}

HTML/JS写法:

<!-- You can custom the "mySwiper" name used to find the swiper instance in current component -->
<template>
  <div v-swiper:mySwiper="swiperOption" @someSwiperEvent="callback">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="banner in banners">
        <img :src="banner">
      </div>
    </div>
    <div class="swiper-pagination"></div>
  </div>
</template>
 
<script>
  export default {
    data () {
      return {
        banners: [ '/1.jpg', '/2.jpg', '/3.jpg' ],
        swiperOption: {
          pagination: {
            el: '.swiper-pagination'
          },
          // some swiper options...
        }
      }
    },
    mounted() {
      setTimeout(() => {
        this.banners.push('/4.jpg')
        console.log('banners update')
      }, 3000)
      console.log(
        'This is current swiper instance object', this.mySwiper, 
        'It will slideTo banners 3')
      this.mySwiper.slideTo(3, 1000, false)
    }
  }
</script>

<style>
	@import "@/assets/css/swiper.css";
</style>

vuewow_154">二、页面滚动加载动画--------vue-wow

安装:npm install vue-wow --save
npm地址:https://www.npmjs.com/package/vue-wow
vue-wow的使用需要引入animate.css文件,因为其动画效果皆由animate.css提供。
vue中使用
1、main.js

// import
import Vue from 'vue'
import "animate.css"
import VueWow from 'vue-wow';
 
// mount with global
Vue.use(VueWow)

2、HTML/JS写法

  <u-animate-container>
    <u-animate
      name="fadeIn"
      delay="0s"
      duration="1s"
      :iteration="1"
      :offset="0"
      animateClass="animated"
      :begin="false" 
    >
      测试
    </u-animate>
  </u-animate-container>
</template>

// mount with component
import {UAnimateContainer, UAnimate} from 'vue-wow'
 
export default {
data () {
      return {
        
      }
    } ,
  components: {
    UAnimateContainer,
    UAnimate
  }
}

②next.js中使用
1、在 plugins 文件夹下新建文件 vue-wow.js

import Vue from 'vue'
import VueWow from 'vue-wow'
 
// mount with global
Vue.use(VueWow)

2、在 nuxt.config.js 文件中配置插件

css: [
    '@/static/css/animate.min.css'
  ],
  plugins: [
   { src: "~/plugins/vue-wow.js", ssr: false }
  ]

HTML/JS写法:

<template>
  <u-animate-container>
    <u-animate
      name="fadeIn"
      delay="0s"
      duration="1s"
      :iteration="1"
      :offset="0"
      animateClass="animated"
      :begin="false" 
    >
      测试
    </u-animate>
  </u-animate-container>
</template>
<script>
  export default {
    data () {
      return {
        
      }
    } 
  }
</script>

vuelazyload_253">三、懒加载-----vue-lazyload

安装:npm install vue-lazyload --save
npm地址:https://www.npmjs.com/package/vue-lazyload
vue中使用

  • main.js
import Vue from 'vue'
import App from './App.vue'
import VueLazyload from 'vue-lazyload'
 
Vue.use(VueLazyload)
 
// or with options
Vue.use(VueLazyload, {
  preLoad: 1.3, //预加载的宽高比
  error: 'dist/error.png', //图片加载失败时使用的图片源
  loading: 'dist/loading.gif', //图片加载的路径
  attempt: 1 //尝试加载次数
})
 
new Vue({
  render: h => h(App),
}).$mount('#app')

  • 在模板中简单使用
<ul>
  <li v-for="img in list">
    <img v-lazy="img.src" >
  </li>
</ul>

②nuxt.js中使用

  • 全局使用
    1、在 plugins 文件夹下新建文件 vue-lazyload.js
import Vue from 'vue'
import VueLazyload from 'vue-lazyload'

Vue.use(VueLazyload)
// or with options
Vue.use(VueLazyload, {
  preLoad: 1.3, //预加载的宽高比
  error: 'dist/error.png', //图片加载失败时使用的图片源
  loading: 'dist/loading.gif', //图片加载的路径
  attempt: 1 //尝试加载次数
})

2、在 nuxt.config.js 文件中配置插件

  plugins: [
   { src: "~/plugins/vue-lazyload.js" ,ssr: false}
  ]

  • 在组件中使用
    不需要新建文件也不要配置文件,直接在组件中使用
    1、组件中引入
import Vue from 'vue'
import VueLazyload from 'vue-lazyload'
Vue.use(VueLazyload)

2、在模板中简单使用

<ul>
  <li v-for="img in list">
    <img v-lazy="img.src" >
  </li>
</ul>

注:小编以为,使用vue-lazyload的缺点在于,图片的大小以及位置无法自定义,它始终都会以父元素大小为准进行拉大或缩小,这样会导致loading图显示十分难看,以及默认图片压缩或扩大,UI不好看。若仅仅是需要加载图片之前显示占位默认图或者是需要图片加载失败时显示默认图,可以将该默认图设置为背景图,得到的是一样的效果。

四、nuxt.js添加百度统计代码

1、在static下的js的文件夹中新建baidu.js文件(当然这个文件名可以随便你取,在此小编就取baidu.js),并将你的百度统计代码放在baidu.js中
2、在nuxt.config.js配置

head: {
    script: [
     {src: '/js/baidu.js'},/*引入百度统计的js*/
    ]
},

http://www.niftyadmin.cn/n/1202757.html

相关文章

input框输入文本限制

1、文本框只能输入数字(小数点也不能输入) <input onkeyup"this.valuethis.value.replace(/\D/g,)" onafterpaste"this.valuethis.value.replace(/\D/g,)" />2、只能输入数字,能输小数点 ①方式一&#xff1a; <input onkeyup"if(isNaN(va…

36、自定义控件详解(一)-- 自定义属性

一、自定义控件分类 1.1、原生控件拓展 修改原有控件我们只需要创建一个类继承该View(ViewGroup)&#xff0c;再原有的逻辑上添加自己的实现即可。 a)文本框(TextView)默认是无法获取焦点的&#xff0c;想让它获取焦点&#xff0c;我们可以通过自定义控件&#xff0c;并重写isF…

好好学习!!

今年开了数据结构和离散数学&#xff0c;听前辈们说这两门课程很重要&#xff0c;也比较难。一定要好好学学&#xff0c;把它学好&#xff01;&#xff01;&#xff01;&#xff01;尽管去年期末考试考得好&#xff0c;但我始终认为我去年没有真正努力过&#xff0c;因为我周末…

js 调用 IOS 、Android原生方法

如今&#xff0c;由于有些页面排版或实现对于android、ios开发人员而言实在麻烦与复杂&#xff0c;实现需要的时间成本太高&#xff0c;导致很多App里都内置了Web网页&#xff0c;app中嵌入Web网页便免不了涉及到Android客户端、IOS客户端与Web网页交互。小编最近就遇到了这个问…

jsp乱码解决

一、JSP页面显示乱码 二、表单提交中文时出现乱码 三、数据库连接 大家在JSP的开发过程中&#xff0c;经常出现中文乱码的问题&#xff0c;可能一至困扰着您&#xff0c;我现在把我在JSP开发中遇到 的中文乱码的问题及解决办法写出来供大家参考。 一、JSP页面显示乱码下面…

dtoj#4299. 图(graph)

题目描述&#xff1a; 对于一个无向图 $G$&#xff0c;三元组 $(a, b, c)$ 被称为优秀的当且仅当满足如下条件&#xff1a; $1. a < b < c$&#xff1b; $2. a $ 与 $b$ 有边相连&#xff1b; $3. a $ 与 $c$ 有边相连&#xff1b; $4. b$ 与 $c$ 没有边相连。 现在有一个…

iframe高度自适应解决方式

带边框的iframe因为能和网页无缝的结合从而不刷新页面的情况下更新页面的部分数据成为可能&#xff0c;可是 iframe的大小却不像层那样可以“伸缩自如”&#xff0c;所以带来了使用上的麻烦&#xff0c;给iframe设置高度的时候多了也不好&#xff0c;少了更是不行&#xff0c;现…

Windows10 安装 Mysql

1、Mysql工具下载&#xff1a;https://dev.mysql.com/downloads/步骤一&#xff1a;步骤二&#xff1a;步骤三&#xff1a; 2、下载Mysql安装包后i.目录下新增my.ini文件和data文件夹 ii.编辑my.ini文件内容&#xff1a;注&#xff1a;安装包存放的目录&#xff0c;文件夹名称不…