前端基础(Vue的模块化开发)

news2024/9/23 21:20:59

目录

前言

响应式基础 ref reactive

学习成果展示

Vue项目搭建

总结 


前言

前面学习了前端HMTL、CSS样式、JavaScript以及Vue框架的简单适用,接下来运用前面的基础继续学习Vue,运用前端模块化编程的思想。

响应式基础 ref reactive

关于ref和reactive,官方解释如下,另外一篇博客写得也很清楚

响应式基础 | Vue.js (vuejs.org)

谈谈Vue3中的ref和reactive_vue reactive_七公子77的博客-CSDN博客

学习成果展示

不用vue框架写一个table

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试用例管理平台</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>

<body>
    <div calss="table" style="
    display: inline-block">
        <h2>测试用例</h2>
        <button class="add-case-button" @click="isEdit=true">
            新增用例
        </button>
        <table border="2">
            <col style="width: 100px; overflow: hidden; text-overflow: ellipsis" />
            <col style="width: 100px" />
            <col style="width: 100px" />
            <col style="width: 100px" />
            <col style="width: 100px" />
            <tr>
                <th>用例名</th>
                <th>步骤名</th>
                <th>关键字</th>
                <th>参数1</th>
                <th>参数2</th>
                <th>操作</th>
            </tr>
            <tr>
                <td>参数正确登录成功</td>
                <td>输入正确的参数登录成功</td>
                <td>用户名 密码</td>
                <td>用户名</td>
                <td>密码</td>
                <td><button>删除</button></td>
            </tr>
            <tr>
                <td>参数错误登录失败</td>
                <td>输入错误的参数登录失败</td>
                <td>用户名 密码</td>
                <td>用户名</td>
                <td>密码</td>
                <td><button>删除</button></td>
            </tr>
            <tr>
                <td>参数为空登录失败</td>
                <td>输入的参数为空登录失败</td>
                <td>用户名 密码</td>
                <td>用户名</td>
                <td>密码</td>
                <td><button>删除</button></td>
            </tr>
            </col>
        </table>
    </div>
</body>

</html>

上面的实现方式用到了很多个th、td标签,维护很麻烦,那有没有更好的解决方法?

如何用vue框架实现?

用v-for,遍历数组中的元素,进行列表的渲染。

关键两行代码:

取列表的表头,表头名称前加上编号,编号从1开始

<th v-for="(Name,key,index) in tableName " :key="key">{{index+1}}{{Name}}</th>

取列表里具体的内容

<tr v-for="testCase in testCases" >

如果要修改表头、列表里内容,不需要在标签里一个一个改,只需要去维护tableName、testCases里的值即可。是不是很省劲!

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试用例管理平台</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>

<body>
    <div id="test">
        <div calss="table" style="
    display: inline-block">
            <h2>测试用例</h2>
            <table border="2">
                <col style="width: 100px; overflow: hidden; text-overflow: ellipsis" />
                <col style="width: 100px" />
                <col style="width: 100px" />
                <col style="width: 100px" />
                <col style="width: 100px" />
                <thead>
                    <tr>
                        <th v-for="(Name,key,index) in tableName " :key="key">{{index+1}}{{Name}}</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="testCase in testCases">
                        <td>{{testCase.caseName}}</td>
                        <td>{{testCase.stepName}}</td>
                        <td>{{testCase.keywords}}</td>
                        <td>{{testCase.param1}}</td>
                        <td>{{testCase.param2}}</td>
                        <td>{{testCase.opration}}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <script>
        const { createApp, computed, ref, reactive } = Vue;
        const = MRJJ{
            setup() {
                let tableName = reactive({
                    "caseName": "用例名",
                    "stepName": "步骤名",
                    "keywords": "关键字",
                    "param1": "参数一",
                    "param2": "参数二",
                    "opration": "操作",
                })

                let testCases = ref([
                    {
                        "id": 1,
                        "caseName": "参数正确登录成功",
                        "stepName": "登录成功",
                        "keywords": "用户名",
                        "param1": "用户名",
                        "param2": "密码",
                        "opration": "删除"
                    },
                    {
                        "id": 2,
                        "caseName": "参数错误登录失败",
                        "stepName": "登录失败",
                        "keywords": "用户名",
                        "param1": "用户名",
                        "param2": "密码",
                        "opration": "删除"
                    },
                    {
                        "id": 3,
                        "caseName": "参数为空登录失败",
                        "stepName": "登录成功",
                        "keywords": "用户名",
                        "param1": "用户名",
                        "param2": "密码",
                        "opration": "删除"
                    },
                ])
                return { tableName, testCases }
            }
        };
        createApp(MRJJ).mount('#test');
    </script>
</body>

</html>

可以看到td标签里的内容自动取出来了。 

踩坑记录:

createApp(MRJJ).mount('#test');

id为test这个div标签里的内容,才能引用MRJJ里面的方法。

结合前面的内容,最终写出来的页面!

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试用例管理平台</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<header id="title">
    欢迎来到MRJJ_9的自动化测试平台
</header>
<body>
    <div class="learn-website">
        <h3>前端学习网站</h3>
        <a class="biglink" href="https://www.w3school.com.cn/html/index.asp">w3school.com</a>
        </br>
        <a href="https://developer.mozilla.org/zh-CN/docs/Web/HTML">MDN 社区</a>
        </br>
        <a class="test01" href="https://www.csdn.net">CSDN社区</a>
        </br>
        <h3>本人博客</h3>
        <a href="https://blog.csdn.net/mrjj_9/category_12393537.html">前端学习博客</a>
    </div>
    <div id="test">
        <div class="form" style="
        display: inline-block">
                <h2 id="form-title">添加测试用例</h2>
                <!-- <button class="close-button" @click="isEdit=false">关闭</button> -->
                <label>
                    用例名:
                    <input type="text" placeholder="输入测试用例名称" name="caseName" v-model="newCase.caseName">
                </label>
                <label>步骤名:
                    <input type="text" placeholder="输入测试步骤名称" name="stepName" v-model="newCase.stepName">
                </label>
                <div>请选择用例类型:
                    <label>
                        <input type="radio" name="type" value="api">接口自动化
                    </label>
                    <label>
                        <input type="radio" name="type" value="ui">UI自动化
                        <br>
                    </label>
                </div>
                <label for="keywords">
                    关键字:
                </label>
                <select name="keywords" v-model="newCase.keywords">
                    <option value="openBrowser">打开浏览器</option>
                    <option value="params">传入必传参数</option>
                </select>
                </br>
                <lable>参数一:<input type="text" name="param1" v-model="newCase.param1"></lable>
                <lable>参数二:<input type="text" name="param2" v-model="newCase.param2"></lable>
            </br>
                <button id="addSubmit" type="button" @click="addCase">提交新增</button>
            </form>
        </div>
        <div calss="table" style="
    display: inline-block">
            <h2>测试用例</h2>
            <table border="2">
                <col style="width: 100px; overflow: hidden; text-overflow: ellipsis" />
                <col style="width: 100px" />
                <col style="width: 100px" />
                <col style="width: 100px" />
                <col style="width: 100px" />
                <thead>
                    <tr>
                        <th v-for="(Name,key,index) in tableName " :key="key">{{index+1}}{{Name}}</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="testCase in testCases">
                        <td>{{testCase.caseName}}</td>
                        <td>{{testCase.stepName}}</td>
                        <td>{{testCase.keywords}}</td>
                        <td>{{testCase.param1}}</td>
                        <td>{{testCase.param2}}</td>
                        <td>
                            <button id="delete" @click="deleteCase(testCase)">删除</button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <script>
        const { createApp, computed, ref, reactive } = Vue;
        const MRJJ = {
            setup() {
                let tableName = reactive({
                    "caseName": "用例名",
                    "stepName": "步骤名",
                    "keywords": "关键字",
                    "param1": "参数一",
                    "param2": "参数二",
                    "opration": "操作",
                })

                let testCases = ref([
                    {
                        "id": 1,
                        "caseName": "参数正确登录成功",
                        "stepName": "登录成功",
                        "keywords": "用户名",
                        "param1": "用户名",
                        "param2": "密码",
                        "opration": "删除"
                    },
                    {
                        "id": 2,
                        "caseName": "参数错误登录失败",
                        "stepName": "登录失败",
                        "keywords": "用户名",
                        "param1": "用户名",
                        "param2": "密码",
                        "opration": "删除"
                    },
                    {
                        "id": 3,
                        "caseName": "参数为空登录失败",
                        "stepName": "登录成功",
                        "keywords": "用户名",
                        "param1": "用户名",
                        "param2": "密码",
                        "opration": "删除"
                    },
                ])
                let newCase = reactive({
                    "caseName": "用例名",
                    "stepName": "步骤名",
                    "keywords": "关键字",
                    "param1": "参数一",
                    "param2": "参数二",
                })
                let isCaseName = ref(true);

                function deleteCase(testCase) {
                    console.log("要删除的用例是:", testCase)
                    testCases.value.splice(testCases.value.indexOf(testCase), 1);
                }

                function addCase() {
                    let lastId = testCases.value[testCases.value.length - 1].id;
                    console.log(lastId);
                    let addCase = { ...newCase };
                    addCase.id = lastId + 1;
                    testCases.value.push(addCase);
                    isEdit.value = false;
                }

                return { tableName, testCases, newCase, addCase, deleteCase}
            }
        };
        createApp(MRJJ).mount('#test');
    </script>
    <link rel="stylesheet" href="case.css">
    <style>

        body {
            background: aliceblue;
            background-image: url("./picture.jpg");
            background-size: 60vw;
            background-position: 10% 10%;
        }
    </style>
</body>
</html>

Vue项目搭建

npm init vue

 

创建的项目结构, 在本地将项目启动起来,进入工程目录,打开终端,输入命令:npm run dev 

 本地启动完成的项目

vue插件安装必备,推荐看下面的这篇博客

开发vue3必备的几个vscode插件,你用上了吗?-腾讯云开发者社区-腾讯云 (tencent.com)

vs code切换主题,File->preferences-Theme

总结 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/900878.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

linux:Temporary failure in name resolutionCouldn’t resolve host

所有域名无法正常解析。 ping www.baidu.com 等域名提示 Temporary failure in name resolution错误。 rootlocalhost:~# ping www.baidu.com ping: www.baidu.com: Temporary failure in name resolution rootlocalhost:~# 一、ubuntu/debian&#xff08;emporary failure i…

提升大数据技能,不再颓废!这6家学习网站是你的利器!

随着国家数字化转型&#xff0c;大数据领域对人才的需求越来越多。大数据主要研究计算机科学和大数据处理技术等相关的知识和技能&#xff0c;从大数据应用的三个主要层面&#xff08;即数据管理、系统开发、海量数据分析与挖掘&#xff09;出发&#xff0c;对实际问题进行分析…

学习笔记230810--get请求的两种传参方式

问题描述 今天写了一个对象方式传参的get请求接口方法&#xff0c;发现没有载荷&#xff0c;ip地址也没有带查询字符串&#xff0c;数据也没有响应。 代码展示 错误分析 实际上这里的query是对象方式带参跳转的参数名&#xff0c;而get方法对象方式传参的参数名是parmas 解…

华为OD机试 - 秘钥格式化 - 双指针(Java 2023 B卷 100分)

目录 专栏导读一、题目描述二、输入描述三、输出描述四、解题思路五、Java算法源码六、效果展示1、输入2、输出3、说明 华为OD机试 2023B卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试&#xff08;JAVA&#xff09;真题&#xff08;A卷B卷&#…

条件判断语句

二、判断语句 # 判断语句result10>15 print("10>5的结果是&#xff1a;%s"% result,type(result))eq"yl""yl" print("\"ylyl\" %s"% eq,type(eq))bool_1True bool_2False print(f"打印出类型,{bool_1},类型是{ty…

【论文笔记】MetaBEV: Solving Sensor Failures for BEV Detection and Map Segmentation

原文链接&#xff1a;https://arxiv.org/abs/2304.09801 1. 引言 目前&#xff0c;多模态融合感知中的一大问题在于忽视了传感器失效带来的影响。之前工作的主要问题包括&#xff1a; 特征不对齐&#xff1a;通常使用CNN处理拼接后的特征图&#xff0c;存在几何噪声时可能导致…

2023-8-14 前缀和

原题链接&#xff1a;前缀和 #include <iostream> using namespace std;const int N 100010;int n, m;int a[N], s[N];int main () {scanf("%d%d", &n, &m);for(int i 1; i < n; i ) scanf("%d", &a[i]);for(int i 1; i < n;…

Oracle Database12c数据库官网下载和安装教程

文章目录 下载安装Oracle自带的客户端工具使用 下载 进入oracle官网 点击下载连接之后右上角会有一个下载 我们只需要数据库本体就够了 运行这个下载器 等待下好之后即可 出现 Complete 之后代表下载成功&#xff0c;然后我们解压即可 安装 双击 双击setup.exe 根据…

windows电脑安装了多个版本python 用vscode编程如何指定版本

在状态栏中找到 Python 版本号。这个版本号表示当前正在使用的 Python 解释器版本。 如果需要切换到其他版本的 Python&#xff0c;请点击版本号&#xff0c;然后从列表中选择所需的 Python 版本。列表中的 Python 版本是按照安装顺序排列的。这里一般会有多个版本可供选择

URL编码指南

URL简介 URL 是统一资源定位符&#xff08;Uniform Resource Locator&#xff09;的缩写。它是用于在互联网上定位并访问资源的一种标识方式。 URL通常由以下几个组成部分组成&#xff1a; 协议&#xff08;Protocol&#xff09;&#xff1a;指示要使用的协议&#xff0c;如…

ubuntu 搜狗输入法安装 和 无法输入中文

一、下载搜狗输入法Linux版本。 搜狗输入法linux-首页 二、安装fcitx输入框架&#xff0c;及相关的依赖库 sudo apt install fcitx-bin sudo apt-get install fcitx-table sudo apt --fix-broken install 三、安装搜狗输入法 sudo dpkg -i sougou....deb 四、通过 设置&…

【福建事业单位-综合基础知识】03行政法

【福建事业单位-综合基础知识】03行政法 1.行政法概述&#xff08;原则重点&#xff09;行政主体范围 行政行为总结 二.行政处罚2.1行政处罚的种类总结 行政法框架 1.行政法概述&#xff08;原则重点&#xff09; 行政法的首要原则是合法&#xff1b;自由裁量——合理行政&…

RequestRespons

文章目录 Request&Respons1 Request和Response的概述2 Request对象2.1 Request继承体系2.2 Request获取请求数据2.2.1 获取请求行数据2.2.2 获取请求头数据2.2.3 获取请求体数据2.2.4 获取请求参数的通用方式 2.3 IDEA快速创建Servlet2.4 请求参数中文乱码问题2.4.1 POST请…

标题:Python数据结构详解:列表、元组、字典和集合

文章目录 &#x1f340;引言&#x1f340;列表&#xff08;List&#xff09;&#xff1a;有序可变序列&#x1f340;特点&#x1f340;常见操作 &#x1f340;元组&#xff08;Tuple&#xff09;&#xff1a;有序不可变序列&#x1f340;特点&#x1f340;常见操作 &#x1f34…

18-组件化开发 根组件

组件化开发 & 根组件: 1. 组件化:一个页面可以拆分成一个个组件&#xff0c;每个组件有着自己独立的结构、样式、行为. 好处:便于维护&#xff0c;利于复用->提升开发效率 组件分类: 普通组件 , 根组件 2. 根组件:整个应用最上层的组件&#xff0c;包裹所有普通小组件…

Smartbi:大模型+ABI在企业数字化浪潮中“推波助澜”?

围绕着大模型进行开发的范式正在形成。 2023年以来&#xff0c;ChatGPT让各界认识到通用大模型的力量。随之而来的&#xff0c;是一场属于所有行业的科技盛宴&#xff0c;一个个行业大模型涌现&#xff0c;几乎所有科技企业都在计划或已经发布了自己的大语言模型&#xff08;L…

ACDU-数据库技术揭秘及应用实践

ACDU-数据库技术揭秘及应用实践 会议时间&#xff1a;2023-08-19 13&#xff1a;30 ~ 17:30 会议地点&#xff1a;杭州西溪万怡酒店 活动主题&#xff1a;本活动由中国数据库联盟组织&#xff0c;并汇集了数据库领域知名人士&#xff0c;一起探讨了数据库前沿技术的应用。 活动…

记录:win10物理机ping不通虚拟机上的docker子网(已解决)

【说明】 windows10&#xff1a;已关闭防火墙 linux发行版本&#xff1a;centos7.9&#xff08;已禁用SElinux、已关闭防火墙&#xff09; 虚拟机软件&#xff1a;VMware Workstation 17 虚拟机网络模式&#xff1a;NAT模式 docker版本&#xff1a;20.4.5 docker网络模式…

当众讲话与演讲口才沙龙活动策划方案

活动名称&#xff1a;当众讲话与演讲口才沙龙 活动目的&#xff1a; 当众讲话与演讲口才沙龙旨在提升参与者的演讲口才能力&#xff0c;培养自信心和表达能力&#xff0c;促进交流与分享。通过举办此活动&#xff0c;我们希望能够帮助参与者克服公众演讲的恐惧&#xff0c;提…

Visual studio的安装教程(最新最详细)新手小白必备

目录 简介 1、Visual Studio下载 2、配置 3、新建项目 4.新建文件 5、选择C文件&#xff08;cpp&#xff09; &#xff0c;命名&#xff0c;选择位置即可 ​编辑 简介 Visual Studio 集成开发环境是一种创新启动板&#xff0c;可用于编辑、调试并生成代码&#xff0c;…