新建SpringBoot项目:Vue3数据绑定显示列表数据(静态数据)

具体如下:

<script>部分的参考代码如下:


<script lang="ts">
import { defineComponent ,onMounted} from 'vue';
import axios from 'axios'

export default defineComponent({
  name: 'Home',
  setup(){

      onMounted(()=>{
        console.log("setup");
        axios.get("http://localhost:8088/ebook/list?name=入门").then((response)=>{
          console.log(response);
        })
    })
  }
});
</script>

接下来要把response参数的内容显示到页面上

定义响应式数据,ref(),可以让javascript数据响应到页面上必须定义为响应式数据。

Home.vue全部参考代码:

<template>
  <div class="home">
    <a-layout>
      <a-layout-sider width="200" style="background: #fff">
        <a-menu
            mode="inline"
            v-model:selectedKeys="selectedKeys2"
            v-model:openKeys="openKeys"
            :style="{ height: '100%', borderRight: 0 }"
        >
          <a-sub-menu key="sub1">
            <template #title>
              <span>
                <user-outlined />
                subnav 1
              </span>
            </template>
            <a-menu-item key="1">option1</a-menu-item>
            <a-menu-item key="2">option2</a-menu-item>
            <a-menu-item key="3">option3</a-menu-item>
            <a-menu-item key="4">option4</a-menu-item>
          </a-sub-menu>
          <a-sub-menu key="sub2">
            <template #title>
              <span>
                <laptop-outlined />
                subnav 2
              </span>
            </template>
            <a-menu-item key="5">option5</a-menu-item>
            <a-menu-item key="6">option6</a-menu-item>
            <a-menu-item key="7">option7</a-menu-item>
            <a-menu-item key="8">option8</a-menu-item>
          </a-sub-menu>
          <a-sub-menu key="sub3">
            <template #title>
              <span>
                <notification-outlined />
                subnav 3
              </span>
            </template>
            <a-menu-item key="9">option9</a-menu-item>
            <a-menu-item key="10">option10</a-menu-item>
            <a-menu-item key="11">option11</a-menu-item>
            <a-menu-item key="12">option12</a-menu-item>
          </a-sub-menu>
        </a-menu>
      </a-layout-sider>
      <a-layout-content
          :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
      >
        Content is update!!!!!
        <pre>
          {{ebook}}
        </pre>

      </a-layout-content>
    </a-layout>
  </div>
</template>

<script lang="ts">
import { defineComponent ,onMounted,ref} from 'vue';
import axios from 'axios'

export default defineComponent({
  name: 'Home',
  setup(){
    const ebook=ref();
      onMounted(()=>{
        console.log("setup");
        axios.get("http://localhost:8088/ebook/list?name=入门").then((response)=>{
          console.log(response);
          const data=response.data;
          ebook.value=data.content;
        })
    })
    return {
        ebook
    }
  }
});
</script>

另一种取回数据到页面的方法是reactive

 导入:

定义对象变量:

 

<template>
  <div class="home">
    <a-layout>
      <a-layout-sider width="200" style="background: #fff">
        <a-menu
            mode="inline"
            v-model:selectedKeys="selectedKeys2"
            v-model:openKeys="openKeys"
            :style="{ height: '100%', borderRight: 0 }"
        >
          <a-sub-menu key="sub1">
            <template #title>
              <span>
                <user-outlined />
                subnav 1
              </span>
            </template>
            <a-menu-item key="1">option1</a-menu-item>
            <a-menu-item key="2">option2</a-menu-item>
            <a-menu-item key="3">option3</a-menu-item>
            <a-menu-item key="4">option4</a-menu-item>
          </a-sub-menu>
          <a-sub-menu key="sub2">
            <template #title>
              <span>
                <laptop-outlined />
                subnav 2
              </span>
            </template>
            <a-menu-item key="5">option5</a-menu-item>
            <a-menu-item key="6">option6</a-menu-item>
            <a-menu-item key="7">option7</a-menu-item>
            <a-menu-item key="8">option8</a-menu-item>
          </a-sub-menu>
          <a-sub-menu key="sub3">
            <template #title>
              <span>
                <notification-outlined />
                subnav 3
              </span>
            </template>
            <a-menu-item key="9">option9</a-menu-item>
            <a-menu-item key="10">option10</a-menu-item>
            <a-menu-item key="11">option11</a-menu-item>
            <a-menu-item key="12">option12</a-menu-item>
          </a-sub-menu>
        </a-menu>
      </a-layout-sider>
      <a-layout-content
          :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
      >
        Content is update!!!!!
        <pre>
          {{ebook1}}
          reactive example:
          {{books}}
        </pre>

      </a-layout-content>
    </a-layout>
  </div>
</template>

<script lang="ts">
import { defineComponent ,onMounted,ref,reactive,toRef} from 'vue';
import axios from 'axios'

export default defineComponent({
  name: 'Home',
  setup(){
    const ebook1=ref();
    const ebook2=reactive({books:[]});
      onMounted(()=>{
        console.log("setup");
        axios.get("http://localhost:8088/ebook/list?name=入门").then((response)=>{
          console.log(response);
          const data=response.data;
          ebook1.value=data.content;
          ebook2.books=data.content;
        })
    })
    return {
        ebook1,
        books:toRef(ebook2,"books")
    }
  }
});
</script>

到ant Design for vue 官网选择一个list

https://2x.antdv.com/components/list-cn

复制代码,进行修改

绑定数据定义:

 以上是没有按官网改动的,会有报错,如下:因为TypeScript是强类型,javascript是弱类型

TypeScrip如果确定不了类型,则使用any类型,任何类型,课解决错误提示。

修改:

  我直接用官网代码修改的参考如下:没有出错。

<template>
  <div class="home">
    <a-layout>
      <a-layout-sider width="200" style="background: #fff">
        <a-menu
            mode="inline"
            v-model:selectedKeys="selectedKeys2"
            v-model:openKeys="openKeys"
            :style="{ height: '100%', borderRight: 0 }"
        >
          <a-sub-menu key="sub1">
            <template #title>
              <span>
                <user-outlined />
                subnav 1
              </span>
            </template>
            <a-menu-item key="1">option1</a-menu-item>
            <a-menu-item key="2">option2</a-menu-item>
            <a-menu-item key="3">option3</a-menu-item>
            <a-menu-item key="4">option4</a-menu-item>
          </a-sub-menu>
          <a-sub-menu key="sub2">
            <template #title>
              <span>
                <laptop-outlined />
                subnav 2
              </span>
            </template>
            <a-menu-item key="5">option5</a-menu-item>
            <a-menu-item key="6">option6</a-menu-item>
            <a-menu-item key="7">option7</a-menu-item>
            <a-menu-item key="8">option8</a-menu-item>
          </a-sub-menu>
          <a-sub-menu key="sub3">
            <template #title>
              <span>
                <notification-outlined />
                subnav 3
              </span>
            </template>
            <a-menu-item key="9">option9</a-menu-item>
            <a-menu-item key="10">option10</a-menu-item>
            <a-menu-item key="11">option11</a-menu-item>
            <a-menu-item key="12">option12</a-menu-item>
          </a-sub-menu>
        </a-menu>
      </a-layout-sider>
      <a-layout-content
          :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
      >
        Content is using list compoent!!!
        <a-list item-layout="vertical" size="large" :pagination="pagination" :data-source="listData">
          <template #footer>
            <div>
              <b>ant design vue</b>
              footer part
            </div>
          </template>
          <template #renderItem="{ item }">
            <a-list-item key="item.title">
              <template #actions>
          <span v-for="{ type, text } in actions" :key="type">
            <component v-bind:is="type" style="margin-right: 8px" />
            {{ text }}
          </span>
              </template>
              <template #extra>
                <img
                    width="272"
                    alt="logo"
                    src="https://gw.alipayobjects.com/zos/rmsportal/mqaQswcyDLcXyDKnZfES.png"
                />
              </template>
              <a-list-item-meta :description="item.description">
                <template #title>
                  <a :href="item.href">{{ item.title }}</a>
                </template>
                <template #avatar><a-avatar :src="item.avatar" /></template>
              </a-list-item-meta>
              {{ item.content }}
            </a-list-item>
          </template>
        </a-list>

      </a-layout-content>
    </a-layout>
  </div>
</template>

<script lang="ts">
import { defineComponent ,onMounted,ref,reactive,toRef} from 'vue';
import { StarOutlined, LikeOutlined, MessageOutlined } from '@ant-design/icons-vue';
import axios from 'axios'

const listData: Record<string, string>[] = [];

for (let i = 0; i < 23; i++) {
  listData.push({
    href: 'https://www.antdv.com/',
    title: `ant design vue part ${i}`,
    avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
    description:
        'Ant Design, a design language for background applications, is refined by Ant UED Team.',
    content:
        'We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
  });
}

export default defineComponent({
  name: 'Home',
  components: {
    StarOutlined,
    LikeOutlined,
    MessageOutlined,
  },
  setup(){
    const ebook1=ref();
    const ebook2=reactive({books:[]});

    const pagination = {
      onChange: (page: number) => {
        console.log(page);
      },
      pageSize: 3,
    };
    const actions: Record<string, string>[] = [
      { type: 'StarOutlined', text: '156' },
      { type: 'LikeOutlined', text: '156' },
      { type: 'MessageOutlined', text: '2' },
    ];


      onMounted(()=>{
        console.log("setup");
        axios.get("http://localhost:8088/ebook/list?name=入门").then((response)=>{
          console.log(response);
          const data=response.data;
          ebook1.value=data.content;
          ebook2.books=data.content;
        })
    })
    return {
        ebook1,
        books:toRef(ebook2,"books"),
        listData,
        pagination,
        actions
    }
  }
});
</script>

补充:

如果需要安装图标库:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来为你具体介绍下springboot+vue实现el-table显示数据的逻辑。 1. 后端逻辑 在后端,我们需要使用Spring Boot框架来实现数据的处理和接口的实现。具体逻辑如下: (1) 定义实体类 首先,我们需要定义实体类来映射数据库表中的数据。例如,我们定义一个User实体类来映射数据库表中的用户数据: ```java public class User { private Long id; private String name; private Integer age; private String address; // getter和setter方法省略 } ``` (2) 定义数据访问接口 接下来,我们需要定义一个数据访问接口来访问数据库中的用户数据。可以使用MyBatis或者Hibernate等ORM框架来实现。例如,在使用MyBatis的情况下,我们可以在Mapper接口中定义一个查询所有用户数据的方法: ```java public interface UserMapper { List<User> getAllUsers(); } ``` (3) 实现数据访问接口 然后,我们需要实现数据访问接口来访问数据库中的用户数据。例如,在使用MyBatis的情况下,我们可以使用Mapper XML文件来实现查询所有用户数据的方法: ```xml <select id="getAllUsers" resultType="User"> SELECT id, name, age, address FROM user </select> ``` (4) 定义控制器 最后,我们需要定义一个控制器来处理前端的数据请求。例如,我们可以定义一个UserController类来处理前端请求获取所有用户数据的方法: ```java @RestController @RequestMapping("/api") public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/users") public List<User> getAllUsers() { return userMapper.getAllUsers(); } } ``` 2. 前端逻辑 在前端,我们需要使用Vue框架来实现页面的渲染和数据的展示。具体逻辑如下: (1) 安装element-ui组件库 我们可以使用npm命令来安装element-ui组件库: ``` npm install element-ui --save ``` (2) 引入el-table组件 在Vue组件中引入el-table组件,并使用axios库从后端接口获取数据。例如: ```vue <template> <div> <el-table :data="users"> <el-table-column prop="id" label="ID"></el-table-column> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> <el-table-column prop="address" label="Address"></el-table-column> </el-table> </div> </template> <script> import axios from 'axios'; export default { data() { return { users: [] } }, mounted() { axios.get('/api/users').then(response => { this.users = response.data; }); } } </script> ``` 其中,`:data="users"`绑定数据源,`el-table-column`定义了要展示的列。在`mounted()`方法中使用axios库从后端接口获取数据,并将数据赋值给`users`变量。 至此,我们就完成了基于Spring Boot和Vue的el-table数据展示功能的实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值