Vue3快速入门+axios的异步请求(基础使用)

news/2024/9/20 14:06:03 标签: vue.js, javascript, 前端

学习Vue之前先要学习html+css+js的基础使用

Vue其实是js的框架

常用到的Vue指令包括vue-on,vue-for,vue-blind,vue-if&vue-show,v-modul

vue的基础模板:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <h1>{{msg}}</h1>
    </div>
    <script type="module">
        import{createApp} from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js';
        createApp({
            data(){
                return{
                    msg:'hello vue3'
                }
            }
        }).mount("#app");
    </script>
</body>
</html>

常用到的Vue指令:

  1. v-for
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
    
        <div id="app">
            <table border="1 solid" colspa="0" cellspacing="0">
                <tr>
                    <th>文章标题</th>
                    <th>分类</th>
                    <th>发表时间</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
    
                <tr v-for="(article,index) in articleList">
                    <td>{{article.title}}</td>
                    <td>{{article.category}}</td>
                    <td>{{article.time}}</td>
                    <td>{{article.state}}</td>
                    <td>
                        <button>编辑</button>
                        <button>删除</button>
                    </td>
                </tr>
    <!--             <tr>
                    <td>标题2</td>
                    <td>分类2</td>
                    <td>2000-01-01</td>
                    <td>已发布</td>
                    <td>
                        <button>编辑</button>
                        <button>删除</button>
                    </td>
                </tr>
                <tr>
                    <td>标题3</td>
                    <td>分类3</td>
                    <td>2000-01-01</td>
                    <td>已发布</td>
                    <td>
                        <button>编辑</button>
                        <button>删除</button>
                    </td>
                </tr> -->
            </table>
        </div>
    
        <script type="module">
            //导入vue模块
            import { createApp} from 
                    'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
            //创建应用实例
            createApp({
                data() {
                    return {
                        articleList:[
                            {
                                title:"医疗反腐绝非砍医护收入",
                                category:"时事",
                                time:"2023-09-5",
                                state:"已发布"
                            },
                            {
                                title:"中国男篮缘何一败涂地?",
                                category:"篮球",
                                time:"2023-09-5",
                                state:"草稿"
                            },
                            {
                                title:"华山景区已受大风影响阵风达7-8级,未来24小时将持续",
                                category:"旅游",
                                time:"2023-09-5",
                                state:"已发布"
                            }
                        ]
                    }
                }
            }).mount("#app")//控制页面元素
        </script>
    </body>
    </html>
  2. v-blind
     
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <div id="app">
            <!-- <a href="https://www.itheima.com">黑马官网</a> -->
             <a v-bind:href="url">黑马</a>
        </div>
        <script type="module">
            //引入vue模块
            import { createApp} from 
                    'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
            //创建vue应用实例
            createApp({
                data() {
                    return {
                        url:'https://www.itheima.com'
                    }
                }
            }).mount("#app")//控制html元素
        </script>
    </body>
    </html>
  3. v-if&&v-show
     
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
        <div id="app">
    
            <!-- 手链价格为:  <span>9.9</span>  <span>19.9</span> <span>29.9</span> -->
    
            <span v-if="customer.level>=0 && customer.level<=1">9.9</span>
            <span v-else-if="customer.level>=2 && customer.level<=4">19.9</span>
            <span v-else>29.9</span>
    
            <br>
            <span v-show="customer.level>=0 && customer.level<=1">9.9</span>
            <span v-show="customer.level>=2 && customer.level<=4">19.9</span>
            <span v-show>29.9</span>
        </div>
    
        <script type="module">
            //导入vue模块
            import { createApp} from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
    
            //创建vue应用实例
            createApp({
                data() {
                    return {
                        customer:{
                            name:'张三',
                            level:3
                        }
                    }
                }
            }).mount("#app")//控制html元素
        </script>
    </body>
    
    </html>
  4. v-on
     
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <div id="app">
            <button v-on:click="money">点我有惊喜</button> &nbsp;
            <button v-on:click="love">再点更惊喜</button>
        </div>
    
        <script type="module">
            //导入vue模块
            import { createApp} from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
    
            //创建vue应用实例
            createApp({
                data() {
                    return {
                        //定义数据
                    }
                },
                methods:{
                    money: function(){
                        alert('123')
                    },
                    love: function(){
                        alert('456')
                    }
                }
            }).mount("#app");//控制html元素
    
        </script>
    </body>
    </html>
  5. v-module
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
        <div id="app">
    
            文章分类: <input type="text" v-modul="searchConditions.category"/><span>{{searchConditions.category}}</span>
    
            发布状态: <input type="text" v-modul="searchConditions.state"/><span>{{searchConditions.state}}</span>
    
            <button>搜索</button>
            <button v-on-click="clear">重置</button>
    
            <br />
            <br />
            <table border="1 solid" colspa="0" cellspacing="0">
                <tr>
                    <th>文章标题</th>
                    <th>分类</th>
                    <th>发表时间</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                <tr v-for="(article,index) in articleList">
                    <td>{{article.title}}</td>
                    <td>{{article.category}}</td>
                    <td>{{article.time}}</td>
                    <td>{{article.state}}</td>
                    <td>
                        <button>编辑</button>
                        <button>删除</button>
                    </td>
                </tr>
            </table>
        </div>
        <script type="module">
            //导入vue模块
            import { createApp } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
            //创建vue应用实例
            createApp({
                data() {
                    return {
                        //定义数据
                        methods:{
                            clear:function(){
                                //清空category里的数据
                            }
                        },
                        searchConditions:{
                            category:'',
                            state:''
                        },
                        articleList: [{
                            title: "医疗反腐绝非砍医护收入",
                            category: "时事",
                            time: "2023-09-5",
                            state: "已发布"
                        },
                        {
                            title: "中国男篮缘何一败涂地?",
                            category: "篮球",
                            time: "2023-09-5",
                            state: "草稿"
                        },
                        {
                            title: "华山景区已受大风影响阵风达7-8级,未来24小时将持续",
                            category: "旅游",
                            time: "2023-09-5",
                            state: "已发布"
                        }]
                    }
                },
                mounted: function() {
                    console.log('vue挂载完毕,发送请求获取数据')
                }
            }).mount("#app")//控制html元素
        </script>
    </body>
    
    </html>

    axios使用:
     

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <!-- 引入axios的js文件 -->
        <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
        <script>
            /* 发送请求 */
            axios({
                method:'get',
                url:'http//localhost:8080/article/getAll'
            }).then(result=>{
                //成功的回调
                //result代表服务器响应的所有数据,包含了响应头,响应体,result.data 代表的是接口响应的核心数据
                console.log(result.data)
            }).catch(err=>{
                //失败的回调
                console.log(err)
            });
    
    /*         // 使用别名的方式发送请求
            axios.get('http//localhost:8080/article/getAll').then(result=>{
                //成功的回调
                //result代表服务器响应的所有数据,包含了响应头,响应体,result.data 代表的是接口响应的核心数据
                console.log(result.data)
            }).catch(err=>{
                //失败的回调
                console.log(err)
            }); */
        </script>
    </body>
    </html>


http://www.niftyadmin.cn/n/5667201.html

相关文章

Python redis 安装和使用介绍

python redis安装和使用 一、Redis 安装1.1、Windows安装 二、安装 redis 模块二、使用redis 实例1.1、简单使用1.2、连接池1.3、redis 基本命令 String1.3.1、ex - 过期时间&#xff08;秒&#xff09;1.3.2、nx - 如果设置为True&#xff0c;则只有name不存在时&#xff0c;当…

Neo4j 简单使用

在 Neo4j 项目中&#xff0c;搭建和使用主要包括以下几个步骤&#xff1a; 1. 安装 Neo4j 首先&#xff0c;安装 Neo4j 可以选择多种方式&#xff0c;包括&#xff1a; 本地安装&#xff1a;在 Windows、macOS 或 Linux 系统中&#xff0c;通过官网下载对应的 Neo4j 安装包。…

深入浅出通信原理

深入浅出通信原理 文章目录 深入浅出通信原理前言一、概述二、信号和频谱2.1 信号2.2 信号的分解与合成2.3 傅里叶变换的特性2.4 离散傅里叶变化 三 信道3.1 衰减和损耗3.2 多普勒效应 四 信源编码4.1 采样4.2 量化4.3 编码 五 基带信号的发送和接受5.1 脉冲成形5.2 眼图 六 频…

运维工程师面试整理-故障排查常见故障的排查步骤及方法

故障排查是运维工程师的重要技能之一。在面试中,面试官通常会通过故障排查相关的问题来评估你解决问题的能力和系统思维。以下是关于常见故障的排查步骤及方法的详细内容,帮助你更好地准备面试。 1. 故障排查的基本步骤 1. 问题识别 a. 描述问题:明确问题的具体表现

React Zustand状态管理库的使用

Zustand 是一个轻量级的状态管理库&#xff0c;适用于 React 和浏览器环境中的状态管理需求。它由 Vercel 开发并维护&#xff0c;旨在提供一种简单的方式来管理和共享状态。Zustand 的设计理念是尽可能简化状态管理&#xff0c;使其更加直观和易于使用。 Zustand 官网点击跳转…

idea多模块启动

文章目录 idea多模块启动2018版本的idea2019版本的idea idea多模块启动 2018版本的idea 1.首先看一下view> Tool Windows下有没有Run Dashboard 如果有&#xff0c;点击一下底部的窗口就会出现 如果不存在&#xff0c;执行下一步 2.查看自己项目的工作空间位置 点击 File&…

【QT】重载信号Connect链接使用方式

有一些Widget有重载的信号&#xff0c;比如QComboBox&#xff1a; void currentIndexChanged(int index) void currentTextChanged(const QString &text)在连接信号槽的时候&#xff0c;需要做一些处理&#xff0c;要表明具体连接的是哪个类型的信号&#xff0c;否则会报错…

JVM java主流的追踪式垃圾收集器

目录 前言 分代垃圾收集理论 标记清除算法 标记复制算法 标记整理法 前言 从对象消亡的角度出发, 垃圾回收器可以分为引用计数式垃圾收集和追踪式垃圾收集两大类, 但是java主流的一般是追踪式的垃圾收集器, 因此我们重点讲解. 分代垃圾收集理论 分代收集这种理…