vue,router,axios练习

简介: vue,router,axios练习

1 绑定数据

 <div id="app">

       {{ message }}

   </div>

   <script>

       var app = new Vue({

           el: '#app',

           data: {

               message: 'Hello elite,welcome to vue2!'

           }

       })

   </script>

2 v-if

<div id="app">

       <button  @click="Click()">click me</button>

       <p v-if="clicked">你点击了按钮</p>

       <p v-else="clicked">没点击</p>

   </div>

   <script>

       var app3 = new Vue({

           el: '#app',

           data: {

               clicked: false

           },

           methods: {

               Click(){

                   this.clicked = true

               }

           },

           })

   </script>

3.v-for

<div id="app">

       <ol>

         <li v-for="(todo,index) in todos" :key="index">

           {{ todo.itemn }}

         </li>

       </ol>

     </div>

     <script>

       var app = new Vue({

           el: '#app',

           data: {

               todos: [

               { itemn: '学习js' },

               { itemn: '学习Vue' },

               { itemn: '学习java' },

               { itemn: '学习python' }

               ]

           }

           })

     </script>

4. v-bind

 <div id="app">

       <ol>

         <!--

           现在我们为每个 todo-item 提供 todo 对象

           todo 对象是变量,即其内容可以是动态的。

           我们也需要为每个组件提供一个“key”,稍后再

           作详细解释。

         -->

         <todo-item

           v-for="item in todoList"

           v-bind:todo="item"

           v-bind:key="item.id"

         ></todo-item>

       </ol>

     </div>

   <script>

      Vue.component('todo-item', {

           props: ['todo'],

           template: '<li>{{ todo.language }}</li>'

           })

           var app7 = new Vue({

           el: '#app',

           data: {

               todoList: [

               { id: 0, language: '学习vue' },

               { id: 1, language: '学习java' },

               { id: 2, language: '学习python' }

               ]

           }

})

   </script>

5.component

<div id="app">

       <todo-item></todo-item>

   </div>

   <script>

          // 定义名为 todo-item 的新组件

          Vue.component('todo-item', {

           template: '<li>这是个li component</li>'

       })

        var app = new Vue({

           el: '#app',

           data: {

               todos: [

               { itemn: '学习js' },

               { itemn: '学习Vue' },

               { itemn: '学习java' },

               { itemn: '学习python' }

               ]

           }

           })

   

   </script>

6.v-model

div id="app">

       <p>{{ message }}</p>

       <input v-model="message">

     </div>

     <script>

           var app6 = new Vue({

           el: '#app',

           data: {

               message: ''

           }

           })

     </script>

7.生命周期

<div id="app">

       <p>{{ message }}</p>

     </div>

     <script>

           var app6 = new Vue({

           el: '#app',

           data: {

               message: '李白'

           },

           created() { // 在渲染之前执行

               console.log("created....")

           },

           mounted() { // 在渲染之后执行

               console.log("mounted....")  

           },

           methods: {

               method1(){

                   console.log("1212")

               }

             

           },

           })

     </script>

8.router

<div id="app">

       <h1>Hello App!</h1>

       <p>

           <!-- 使用 router-link 组件来导航. -->

           <!-- 通过传入 `to` 属性指定链接. -->

           <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->

           <router-link to="/">首页</router-link>

           <router-link to="/user">用户管理</router-link>

           <router-link to="/menu">菜单管理</router-link>

       </p>

       <!-- 路由出口 -->

       <!-- 路由匹配到的组件将渲染在这里 -->

       <router-view></router-view>

   </div>

   <script>

       // 1. 定义(路由)组件。

       // 可以从其他文件 import 进来

       const Welcome = { template: '<div>欢迎</div>' }

       const User = { template: '<div>user list</div>' }

       const Menu = { template: '<div>menu list</div>' }

       // 2. 定义路由

       // 每个路由应该映射一个组件。

       const routes = [

           { path: '/', redirect: '/welcome' }, //设置默认指向的路径

           { path: '/welcome', component: Welcome },

           { path: '/user', component: User },

           { path: '/menu', component: Menu }

       ]

       // 3. 创建 router 实例,然后传 `routes` 配置

       const router = new VueRouter({

           routes // (缩写)相当于 routes: routes

       })

       // 4. 创建和挂载根实例。

       // 从而让整个应用都有路由功能

       const app = new Vue({

           el: '#app',

           router

       })

       // 现在,应用已经启动了!

   </script>

9.axios

json数据:

{

 "data":[

   {

      "username":"elite",

      "age":12

   },

   {

       "username":"marry",

       "age":14

   }

 ]

<div id="app">

       <div>axios demo</div>

       <!--循环列表-->

       <table border="1">

           <tr>

               <td>姓名</td>

               <td>年龄</td>

           </tr>

           <tr v-for="(item,index) in userList">

               <td>{{item.username}}</td>

               <td>{{item.age}}</td>

           </td>

       </tr>

   </div>

   <script>

       var app = new Vue({

       el: '#app',

       data: {

           userList: []//数组

       },

       created() {

           this.getUserList()

       },

       methods: {

         

           getUserList(id) {

               debugger;

               //vm = this

               axios.get('data.json')

               .then(response => {

                   console.log(response)

                   this.userList = response.data.data

               })

               .catch(error => {

                   console.log(error)

               })

           }

       }

   })

   </script>

723ce4c399be4b9a8f5703a39816782d.png


相关文章
|
1月前
|
移动开发 资源调度 JavaScript
Vue Router的介绍与引入
Vue Router的介绍与引入
17 0
|
23天前
|
前端开发 JavaScript
1天搞定SpringBoot+Vue全栈开发 (7)Axios网络请求
1天搞定SpringBoot+Vue全栈开发 (7)Axios网络请求
|
1月前
|
JavaScript 安全 网络架构
阿珊详解Vue Router的守卫机制
阿珊详解Vue Router的守卫机制
|
1月前
|
JavaScript 网络架构 开发者
阿珊解说Vue中`$route`和`$router`的区别
阿珊解说Vue中`$route`和`$router`的区别
|
1月前
|
移动开发 JavaScript 前端开发
Vue Router的介绍与引入
Vue Router的介绍与引入
|
1月前
|
JSON JavaScript 前端开发
< 每日份知识快餐:axios是什么?如何在Vue中 封装 axios ? >
本文介绍了前端开发中常用的HTTP客户端库Axios,它基于Promise,支持浏览器和Node.js,特点是功能强大、支持Promise API和并发请求,并能拦截请求和响应。文章强调了理解Axios的内部原理和优化使用的重要性,不仅讲解了基本的安装、导入和使用方法,还阐述了为何选择Axios,包括其丰富的配置选项和良好的浏览器支持。此外,文章探讨了封装Axios的必要性,以减少重复代码和提高代码维护性,并给出了设置接口请求前缀、请求头、超时时间以及封装请求方法和拦截器的示例。通过封装,开发者可以更高效地管理和使用Axios,适应不同项目需求。
|
1月前
|
资源调度 JavaScript 前端开发
路由管理:Vue Router的使用和配置技巧
【4月更文挑战第22天】Vue.js的官方路由管理器Vue Router简化了单页面应用的路由管理。通过定义路由组件和映射URL,它实现了页面导航和组件加载。安装Vue Router后,在`src/router/index.js`配置路由,如`{ path: &#39;/&#39;, component: Home }`。使用`&lt;router-view&gt;`渲染组件,`&lt;router-link&gt;`进行导航。动态路由匹配允许同一个组件对应不同URL。嵌套路由和编程式导航进一步增强路由功能。路由守卫可在路由切换时执行逻辑,而路由懒加载能按需加载组件,提升性能。
|
1月前
|
JSON 前端开发 JavaScript
Vue+Axios+SpringBoot后端同时接收文件和json作为请求参数
Vue+Axios+SpringBoot后端同时接收文件和json作为请求参数
54 0
|
1月前
|
JavaScript
vue中router页面之间参数传递,params失效,建议使用query
vue中router页面之间参数传递,params失效,建议使用query
13 0