vue笔记(5)

本文介绍了Vue.js中的$refs使用方法,用于聚焦文本框和组件动态显示隐藏。还讲解了数组过滤reduce方法,组件动态加载,v-slot及keep-alive的运用,以及自定义指令的创建。此外,还提到了axios的全局配置和在Vue 2与Vue 3中的使用方式。

$refs 如何使用

<input type="text" v-if="inputVisible" @blur="showButton" ref="iptRef" />  blue焦点离开
    <button v-else @click="showInput">展示输入框</button>  

howInput() {
      // 1. 切换布尔值,把文本框展示出来
      this.inputVisible = true
      // 2. 让展示出来的文本框,自动获取焦点
      this.$nextTick(() => {        数据更新后执行 this.$nextTick(()=>{})括号中是回调函数
        this.$refs.iptRef.focus()
      })
    },
showButton() {
      this.inputVisible = false   焦点离开隐藏
    },

编码规范 先指令 v-xx 后 属性 : xx 在事件 @xx

reduce

 return this.list
      .filter(item=>item.goods_state)
      .reduce((e,item)=>e+=item.goods_count,0)


list.reduce((x,item)=>运算式,默认值)  x为自定义变量  item==list

components标签 可动态显示或隐藏组件

通过切换变量值来实现组件动态显示/隐藏

 <button @click='comName="Left"'>Left</button>
 <button @click='comName="Right"'>Right</button>
 <components :is='comName'></components>

v-solt 及name  只能用在template中

v-slot :'  '==#            <slot name="defau"></slot>   

                                 <template v-slot:defau/#defau></template>  

keep-alive    将内部组件进行缓存而不是销毁

include保存Left组件exclude只销毁Right  两者只能同时存在一个
<keep-alive include="Left" include='Left'/exclude=Right>
          <components :is='comName' ></components>
</keep-alive>

插槽应用 爷孙传递数据

  爷        <Counter   :num='item.goods_count' @num-change='getadd(item)'></Counter>
  父               <slot></slot>
  孙     接受数据即可使用  props:{"num"}     
  

directives自定义指令 

私有自定义指令的节点

私有自定义指令的节点
  directives: {
    // 定义名为 color 的指令,指向一个配置对象
    /* color: {
      // 当指令第一次被绑定到元素上的时候,会立即触发 bind 函数
      // 形参中的 el 表示当前指令所绑定到的那个 DOM 对象
      bind(el, binding) {
        console.log('触发了 v-color 的 bind 函数')
        el.style.color = binding.value
      },
      // 在 DOM 更新的时候,会触发 update 函数
      update(el, binding) {
        console.log('触发了 v-color 的 update 函数')
        el.style.color = binding.value
      }
    } */
    color(el, binding) {
      el.style.color = binding.value      包含bind  update功能
    }   
  }

公有自定义指令的节点   main.js中创建

// 全局自定义指令
 Vue.directive('color', {
  bind(el, binding) {
    el.style.color = binding.value
  },
  update(el, binding) {
    el.style.color = binding.value
  }
}) 

Vue.directive('color', function(el, binding) {
  el.style.color = binding.value
})

定义全局axios与配置全局axios的根路径 不利于复用

vue2

import Vue from 'vue'
import axios from 'axios'
配置axios全局根路径
axios.defaults.baseURL='http://www.liulongbin.top:3006'
配置axios为全局变量
Vue.prototype.$http=axios   $http为自定义名称 

组件中使用 
    get   const {data:res}=await this.$http.get('api/get')
    post  const {data:res}=await this.$http.post('api/post')
   

vue3

import { createApp } from 'vue'
import App from './App.vue'
import axios from 'axios'
const app=createApp(App)
配置axios全局根路径
axios.defaults.baseURL='http://www.liulongbin.top:3006'
配置axios为全局变量
app.config.globalProperties.$http=axios
app.mount('#app')

  请求数据时 get使用 params(要查询的属性值)     post使用data(要提交的数据) 

   this.$http.get('http://www.liulongbin.top:3006/api/get',{
    params:{
            name: 'zs',
            age:18
}}),
   this.$http.post('http://www.liulongbin.top:3006/api/get',{
    data:{
            name: 'zs',
            age:18
}})

### 关于 Vue 的学习笔记与教程 以下是综合整理的关于 Vue 及其生态系统的相关内容,涵盖了基础概念、常用功能以及实际开发中的技巧。 #### 1. Vue Router 使用指南 Vue Router 是 Vue 官方支持的一个路由管理库,用于构建单页面应用 (SPA)[^1]。通过它可以在不重新加载整个页面的情况下实现导航切换。基本配置如下: ```javascript import Vue from 'vue'; import Router from 'vue-router'; // 安装插件 Vue.use(Router); const routes = [ { path: '/', name: 'Home', component: () => import('@/views/Home.vue') // 按需加载组件 }, { path: '/about', name: 'About', component: () => import('@/views/About.vue') } ]; export default new Router({ mode: 'history', // 移除 URL 中的 # base: process.env.BASE_URL, routes }); ``` 上述代码展示了如何定义路径及其对应的组件,并设置历史模式来优化用户体验[^3]。 #### 2. 注册全局组件和指令 为了提高代码复用性和可维护性,在项目中可以注册一些通用的功能模块作为全局可用资源[^2]。例如下面的例子演示了创建自定义按钮组件及焦点自动获取指令的过程: ```javascript // 注册全局组件 Vue.component('my-button', { data() { return { clickCount: 0 }; }, methods: { incrementClicks() { this.clickCount++; } }, template: '<button @click="incrementClicks">已点击{{ clickCount }}次</button>' }); // 注册全局指令 Vue.directive('auto-focus', { inserted(el) { el.focus(); } }); ``` 这段脚本允许开发者在整个应用程序范围内轻松调用 `my-button` 和 `v-auto-focus` 功能而无需重复声明它们的位置。 #### 3. Axios 数据请求处理 Axios 提供了一种简单的方式来发送 HTTP 请求并与服务器交互[^4]。以下是一个典型的 POST 方法示例,展示如何向指定端点提交数据并捕获响应结果: ```javascript axios.post('/api/user/login', { username: 'admin', password: 'password' }) .then((response) => { console.log(`登录成功! 用户信息:${JSON.stringify(response.data)}`); }) .catch((error) => { console.error('发生错误:', error); }); ``` 此片段说明了当用户尝试登录时应采取的操作流程——如果一切正常,则打印返回的数据;反之则记录异常详情以便调试分析。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值