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
相关推荐
@大迁世界1 小时前
Firefox 本地模式:localhost 再见了 ?
前端·架构
米米商聊3 小时前
同名附件为什么被 Map 覆盖?重复键与一对多索引
前端·javascript
echome8884 小时前
Python 异常处理:try、else、finally 的正确搭配
开发语言·前端·python·异常处理·编程技巧
时间的情敌5 小时前
OpenLayers 实战|Vue3 + OpenLayers 完整工程案例(加载天地图、要素、弹窗、绘制、WFS)
前端·openlayers
陈随易5 小时前
下载神器!Motrix 停更三年后重写回归
前端·后端·程序员
前端的日常5 小时前
一个人+AI做情侣食谱小程序,0 费用的服务器
前端·后端
计算机魔术师6 小时前
谷歌搜索广告踩刹车,AI广告成为救命稻草
前端
总要学点什么6 小时前
第81篇:数据可视化-PM2.5分布——让圆点的胖瘦替每座城市报出浓度
前端·信息可视化·echarts·cesium·空间分析
IT_陈寒6 小时前
Vue的v-for里用了个临时变量,页面直接挂了
前端·人工智能·后端
hunter_0766 小时前
一次 iOS 真机签名 409 的完整排查:证书、设备、描述文件为什么总是"对不上"
前端