vue-cil组件的定义和使用、登录小案例

定义使用组件

定义组件

  1. 在 components 文件夹中创建Vue文件
  2. 在<template><\template>中写组件样式
  3. 在<script><script>中写导出语法
  4. 在导出语法中写组件的配置项vue
html 复制代码
<script>
export default {
    name:'myzujian', //组件名字
    data(){
        return {myname:"caimin"}
    }
}
</script>
<template>
<div>组件1{{myname}}</div>
</template>

<style></style>

使用组件

  1. 在使用组件的位置导入组件
  2. 在components中注册组件
html 复制代码
<template>
  <div class="home">
    <h1>我的组件</h1>
      <myzujian></myzujian>   //使用组件
  </div>
</template>

<script>
import myzujian from "@/components/myzujian.vue";  //导入组件
export default {
  name: 'HomeView',
  components: {
    myzujian   // 注册组件
  }
}
</script>

vue项目是如何执行起来的

入口 main.js 导入了vue和App.vue

  1. 用代码做了关联:index.html 的 id 为 app 的 div,以后,所有的代码,都写在App.vue中,按照vue的开发规范写
  2. main.js 做的,所以它是入口

访问路由显示不同组件

使用路由 :在App.vue中写一个标签<router-view/>

路由表情的配置就在router文件夹中的index文件中

配置路由

在index文件夹中通过在const routes添加对象实现

  1. 将组件导入index文件
  2. 在中添加对象
python 复制代码
{
    path: '/',    // 根路由,斜杆后面写路由
    name: 'xxx',  
    component: HomeView  // 注册组件 
},

其间组件中的通信和ref属性和之前一样

就可以实现通过访问不同的路由显示不同的组件

登录小案例

vue-cil中登录组件

html 复制代码
<script>
import axios from "axios";

export default {
    name: 'Login',
    data() {
        return {
            username: '',
            password: ''
        }
    },
    methods: {
        handlelogin() {
            axios.post('http://127.0.0.1:8000/login', {
                username: this.username,
                password: this.password
            }).then(res => {
                if (res.data.code == 100) {
                    // 跳转到某个页面
                    console.log('登录成功')
                    this.$router.push(res.data.url)
                } else {
                    alert(res.data.msg)
                }
            })
        }
    }
}

</script>

<template>
    <div class="login">
        <h1>登录页面</h1>
        <p>用户名:<input type="text" v-model="username"></p>
        <p>密码:<input type="password" v-model="password"></p>
        <button @click="handlelogin">登录</button>
    </div>
</template>

<style></style>

django后端交互

python 复制代码
class LoginView(APIView):
    def post(self, request):
        if request.data.get('username') == 'cai1' and request.data.get('password') == '111':
            res = Response(data={'code': 100, 'msg': '成功', 'url': 'home'})
        else:
            res = Response(data={'code': 101, 'msg': '用户名密码错误'})
        res['Access-Control-Allow-Origin'] = '*'
        return res
相关推荐
卸任14 小时前
Vibe coding工具使用心得
前端·ai编程
brien915 小时前
为什么模型标称 1M,Google Antigravity 聊到 256K 却突然失忆?我们给它做了个原生行内监控圆环
前端
修炼的dance15 小时前
录屏关键帧间隔多少合适?六档实测,2秒到10秒小了75.6%
前端
Lank_M15 小时前
关键帧从1秒拉到10秒,录屏小87.3%,摄像头小10.8%
前端
补码缩写15 小时前
用 ffprobe 拆录屏,八成字节在 29 个关键帧上
前端
百思不得小李15 小时前
一个复杂的动态表单是如何实现的
前端·设计模式
陈随易16 小时前
Cmder 停更 17 个月,2.0 支持 Windows Terminal
前端·后端·程序员
清晨Coding16 小时前
删了 Cookie 还能自动登录?Google系网站登录态自动恢复的原理分析
javascript
风骏时光牛马16 小时前
前沿视界
前端
kyriewen16 小时前
我扫了 814 个依赖,97 个还敢要 Node 10
前端·javascript·程序员