VUE知识整理

2022-10-06  本文已影响0人  云顶天宫写代码

安装路径不要加空格!!!!!!!

下载 https://nodejs.org/en/

image.png image.png

修改全局缓存路径

npm config set registry https://registry.npm.taobao.org
npm config set prefix "D:\nodejs\node_global"
npm config set cache "D:\nodejs\node_cache"
npm install -g yarn --registry=https://registry.npm.taobao.org

如果安装的node 是17以上,对 yarn 的指令执行有影响
需在命令行执行 $env:NODE_OPTIONS="--openssl-legacy-provider"

.vue 文件解释

Vue 的单文件组件 (即 *.vue 文件,英文 Single-File Component,简称 SFC) 是一种特殊的文件格式,使我们能够将一个 Vue 组件的模板、逻辑与样式封装在单个文件中。下面是一个单文件组件(MyCup.vue)的示例:

<template>
  <div>
  <!-- 这里是vue的html代码片段 -->
  </div>
</template>
  
<script>
// js代码片段,通常使用ES6语法写
export default {
  name: "MyCup", // 组件的名字,和文件名字一致
  props: {
    //这里是自定义的属性 :前面是自定义的属性名,后面是属性类型
  },
  data() {
    // 管理对象的数据项, 是一个对象
    return {
        type:'玻璃杯',
        weight:'200g'
    };
  },
  watch: {
    // 监听data中的数据项,方法名字 和data()中的变量名字一样
  },
  methods: {
// 定义方法,作为template 中的元素事件绑定
      water(){
          
      }
  },
};
</script>
  
  <!-- Add "scoped" attribute to limit CSS to this component only -->
  <style scoped>
/* 这里是内部的style的样式 */
</style>

一、项目脚手架

二、VUE相关概念

<template>
  <div class="about">
    <el-form label-position="left" label-width="100px" style="max-width: 460px">
      <el-form-item label="用户名">
        <el-input v-model="formObj.username" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="formObj.password" />
      </el-form-item>
      <el-form-item>
        <!-- 使用动态跳转方式 -->
        <el-button type="primary" @click="submitForm()">立即登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "LoginView",
  data() {
    return {
      // 在 eslint 中的标准的对象,k-value 中的 value通常需要使用 单引号
      formObj: {
        username: "",
        password: "",
      },
    };
  },
}

在template 中不用写 this

export default {
  data() {
    return {
      question: '',
      answer: 'Questions usually contain a question mark. ;-)'
    }
  },
  watch: {
    // 每当 question 改变时,这个函数就会执行
    question(newQuestion, oldQuestion) {
      if (newQuestion.includes('?')) {
        this.getAnswer()
      }
    }
  },
  methods: {
    async getAnswer() {
      this.answer = 'Thinking...'
      try {
        const res = await fetch('https://yesno.wtf/api')
        this.answer = (await res.json()).answer
      } catch (error) {
        this.answer = 'Error! Could not reach the API. ' + error
      }
    }
  }
}

声明混入的代码
// 这里和组件中的 export default{} 中的属性平级关系
export const score = {
  methods: {
    alertScore(student, score1) {
      alert(student + "的总成绩:" + score1);
    },
  },
};

局部混入的使用


image.png

作用域插槽:商品列表(elementUI 中的table组件)
数据在子组件中,但是自己不能决定结构,需要将数据传递给父组件,父组件决定结构

三、路由(vue-router)

路由是前端框架定制化的一种对界面跳转的新叫法

// 路由懒加载,延迟加载 import中的值是 路径地址
component: () => import("../layout/BaseLayout.vue")

image.png

多用于嵌套路由

image.png

跳转的页面接收:


image.png

第二种: push方法

多用于单页面间的跳转

常规用法


image.png

传递参数的用法


image.png

小程序使用的 uni.navigateTo : 跳转单页面
uni.switchTab : 跳转到tab导航页面

// to:要跳转的导航地址   from:当前页面    next:执行跳转页面的方法
router.beforeEach((to, from, next) => {
   // 判断是否登录过了,如果是,跳转到 to 传入的页面地址  反之,拦截页面跳转,还停留在登录界面
  if (to.name !== 'Login' && !isAuthenticated){
    next({ name: 'Login' })
  }else{
    next()
  }
})

全局的后置守卫

router.afterEach((to, from, failure) => {
  if (!failure) sendToAnalytics(to.fullPath)
})
const routes = [
  {
    path: '/posts',
    component: PostsLayout,
    children: [
      {
        path: 'new',
        component: PostsNew,
        // 只有经过身份验证的用户才能创建帖子
        meta: { requiresAuth: true }
      },
      {
        path: ':id',
        component: PostsDetail
        // 任何人都可以阅读文章
        meta: { requiresAuth: false }
      }
    ]
  }
]

用到的组件库

  1. element-ui 2.2
  2. uView 2.0
上一篇 下一篇

猜你喜欢

热点阅读