期末模拟题

该文章展示了一个Vue.js组件,用于用户输入产品名称并进行数据验证,确保名称非空且不包含数字。当验证成功后,应用会通过axios库向后端发起请求获取产品列表,并使用router配置在不同页面间导航。同时,文章还展示了响应式表格展示搜索结果的示例代码。
摘要由CSDN通过智能技术生成

数据验证约束

<template>

  <div>

    <div id="search">

      <div>产品名称</div>

      <div><input type="text" placeholder="请输入产品名称" v-model="name"></div>

    </div>

    <div id="error">{{ errormsg }}</div>

    <div id="submit"><input type="button" value="录入"  @click="checkdata"></div>

  </div>

</template>

<script>

import '../assets/input.css'

export default {

  data() {

    return {

      name:"",

      errormsg:""

    }

  },

  methods:{

    checkdata(){

      if(this.name==""){

        this.errormsg="请输入产品名称"

      }

      else if(/\d/.test(this.name)){

        this.errormsg="产品名称中不能有数字"

      }else{

        this.$router.push('/product')

      }

    }

  }

}

</script>

<style scoped>

#search div:first-child{

  float: left;

  width: 100px;

  text-align: right;

  margin-right: 10px;

}

#submit input{

  margin-top: 15px;

  width: 268px;

}

#error{

  color: #ff0000;

}

</style>

前后端数据交互:点击页面中“搜索”按钮向后端发起请求

<template>

    <div>

        <div><input type="text" id="product" placeholder="请输入产品名称">

            <input type="button" id="search" value="搜索">

        </div>

        <div id="product">

            <table>

                <tr>

                    <th>图片</th>

                    <th>品牌</th>

                    <th>型号</th>

                    <th>价格</th>

                </tr>

                <tr v-for = "item in list" :key="item" >

                    <th><img :src =" 'http://114.67.241.121:8080/img/' +item.image"></th>

                    <th>{{item.brand}}</th>

                    <th><a href="'http://114.67.241.121:8080/img/'+item.image">{{ item.model }}</a>

                    </th>

                    <th>{{ item.price }}</th>

                </tr>

            </table>

        </div>

    </div>

</template>

<script>

export default {

    data() {

        return {

            list: []

        }

    },

    created() {

        this.axios({

            method: "get",

            url: "http://114.67.241.121:8080/product/list"

        }).then(res => {

            console.log(res)

            this.list = res.data.data

        })

    }

}

</script>

<style scoped>

table{

    border: 1px solid #000;

    border-collapse: collapse;

    width: 550px;

}

th{

    height: 30px;

    text-align: center;

    vertical-align: middle;

    border: 1px solid #000;

}

td{

    height: 100px;

    text-align: center;

    vertical-align: middle;

    border: 1px solid #000;

}

img{

    height: 100px;

    width: 100px;

}

a{

    color: #00ff00;

}

a:hover{

    color: #ff0000;

}

tr th:nth-child(4){

    background-color: #ffffd0;

}

tr td:nth-child(4){

    background-color: #ffffd0;

}

tr th:nth-child(1){

   width: 100px;

}

tr th:nth-child(2){

   width: 150px;

}

tr th:nth-child(4){

   width: 150px;

}

</style>

router配置

         

import Vue from 'vue'

import Router from 'vue-router'

import input from '@/components/input'

import product from '@/components/product'

Vue.use(Router)

export default new Router({

  routes: [

    {

      path: '/',

      name: 'input',

      component: input

    },

    {

      path: '/product',

      name: 'product',

      component: product

    }

  ]

})

axios

import axios from 'axios'

Vue.prototype.axios=axios

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值