vue day(3) 过渡,生命周期,自定义指令

一、transition过渡

1.1 单元素过渡

使用<transaition></transition>标签进行控制

1.2 多元素过渡

  • 多元素的意思是多个元素中一次只能选中一个元素进行动画展示
  • 前提是多个元素的key值不一样,或者标签的类型不同,不然不会有动画效果。
  • mode:out-in/in-out,来控制动画切换的方式

1.3 多个组件过渡

和多元素过渡方式一样

1.4 列表过渡

  • 使用<transaition-group tag="ul"></transition-group>,不指定tag,外面会包裹一个span标签。
  • 记得设置一个key值,不是在<transaition-group tag="ul"></transition-group>这里面设置。

二、vue生命周期,过滤器,自定义指令

2.1 vue生命周期

  • beforeCreated
  • created
  • beforemounted
  • mounted
    上面的4个事件在组件的生命周期中只会触发一次
  • beforeupdated
  • updated
    组件中的一些状态更新时会触发这两个事件。
  • beforedestoryed
  • destoryed
    组件被销毁的时候会触发这两个事件。

2.2 过滤器

  • 使用过滤器
    | 右边是定义的过滤器的名称
  <img :src="item.cover | myfilter"/>

自定义的过滤器

 Vue.filter("myfilter",function(path){
                return path.replace('png','png@200w_268h.png');
			 })
  • 举例
    设置div的title,在过滤器过滤后加上"aaa"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
    <div style="width:100px;height:200px;border: 1px solid red" :title="title | myfilter"></div>
    <button @click="show()">按钮</button>
</div>
    
<script>
  Vue.filter("myfilter",function(data){
    return data + "aaaaa"
  })
 var vm = new Vue({
  el: '#app',
  data: {
    title:"hahah"
  },
  methods: {
    show(){
      console.log(this.title)
    }
  }
})
</script>
</body>
</html>

2.3 vue.swiper

没学过,跳过

2.4 自定义指令

  • 在 Vue.directive中自定义指令

inserted方法只执行一次,update方法会执行多次,只有inserted方法,通过变量是修改不了状态的。


  Vue.directive("hello",{
			inserted(el,bind){
				console.log(bind.value);
				console.log(bind)
              el.style.background = bind.value;
			},
			update(el,bind) {
				el.style.background = bind.value;
			}})
			var vm = new Vue({
				el:"#start",
				data:{
					color:"red"
				}
			})
  • 使用自定义指令
 <div v-hello="color">111111</div>

2.5 单文件组件

组件开发在.vue文件中,webpak会帮我们去解析,但是需要我们自己去配置一些内容,vue的脚手架已经帮我们配置好了,我们直接去使用就可以。

三、vue脚手架创建vue项目

3.1 安装脚手架

  • 旧的需要先卸载
npm uninstall vue-cli -g
npm install -g @vue/cli

3.2 vue create proname

  • 创建vue项目

3.3 环境构建

  • npm run serve
    开发环境构建

  • npm run build
    生产环境构建

  • npm run lint
    代码检测工具

3.4 测试

  • cd proname
    进入创建的vue项目中
  • npm run serve
    开发环境构建
    在这里插入图片描述
  • 访问: http://localhost:8080
    在这里插入图片描述

3.5 单文件组件

<template>
  <div>
    <div>hello my dad</div>
    <input type="text" ref="myText">
    <button @click="handleClick()">按钮</button>
    <ul>
      <li v-for="data in name" :key="data">
        {{data}}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data () {
    return {
      name: []
    }
  },
  methods: {
    handleClick () {
      this.name.push(this.$refs.myText.value)
      console.log('hahha')
    }
  }
}
</script>

<style lang="scss">
ul{
  li{
    background:red;
  }
}
</style>

  • 效果 
    在这里插入图片描述

3.6 全局组件导入方式

<script>
// 全局组件导入方式
import navbar from './components/Navbar'
import sidebar from './components/SideBar'
import Vue from 'vue'
Vue.component('navbar', navbar)
Vue.component('sidebar', sidebar)
</script>

3.7 局部组件导入方式


<script>
// 局部组件导入方式
import navbar from './components/Navbar'
import sidebar from './components/SideBar'
export default {
  data () {
    return {
      name: []
    }
  },
  methods: {
    handleClick () {
      this.name.push(this.$refs.myText.value)
      console.log('hahha')
    }
  },
  components: {
    navbar: navbar,
    sidebar: sidebar
  }
}
</script>

3.8 scoped属性

加上scoped属性,样式局部生效,即彼此之间样式不会相互影响

<style scoped>

</style>

3.9 esclint工具

  • npm run lint
    在项目中,使用该命令,会自动修复一些因为格式带来的错误。

在这里插入图片描述

  • npm run build
    会将前端项目进行打包,最终生成dist文件夹
    在这里插入图片描述
  • dist文件夹

在这里插入图片描述

3.10 vue反向代理配置

  • 安装axios
cnpm install save axios
  • 倒入axios
import axios from 'axios'
  • 创建vue.config.js文件
module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:8081/sendMail',
        ws: true,
        // https请求需要该设置
        secure: false,
        changeOrigin: true,
        pathRewrite: { '^/api': '' }
      }
    }
  }
}
  • 请求发送
  mounted () {
    axios.post('/api', {
      email: '401129874@qq.com'
    }).then(res => console.log(res))
  }
  • 结果
    在这里插入图片描述

3.11 配置完后需要重启一下项目

  • npm run serve
    记得把修改过的文件都保存一下,该关闭的关闭了,不然会报错
module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: 'https://monitor.maoyan.com',
        changeOrigin: true
      }
    }
  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值