1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    6. <title></title>
    7. <script src="https://unpkg.com/vue@next"></script>
    8. <!-- 引入样式 -->
    9. <link
    10. rel="stylesheet"
    11. href="https://unpkg.com/element-plus/lib/theme-chalk/index.css"
    12. />
    13. <!-- 引入组件库 -->
    14. <script src="https://unpkg.com/element-plus/lib/index.full.js"></script>
    15. </head>
    16. <body>
    17. <div id="root"></div>
    18. </body>
    19. <script>
    20. // 文本框相关逻辑
    21. const useInputEffect = () => {
    22. const { ref } = Vue;
    23. const inputValue = ref("");
    24. const handleInputChange = (newValue) => {
    25. inputValue.value = newValue; // newValue就是用户输入的值
    26. };
    27. return { inputValue, handleInputChange };
    28. };
    29. // 列表相关逻辑
    30. const useListEffect = () => {
    31. const { reactive } = Vue;
    32. // 用来存所有的待办事项
    33. const list = reactive([]);
    34. const addItemToList = (item) => {
    35. list.push({ content: item.value }); // 向列表中加入待办事项
    36. item.value = ""; // 清空文本框的值
    37. };
    38. return { list, addItemToList };
    39. };
    40. const app = Vue.createApp({
    41. setup() {
    42. // setup函数只做流程中转 引用定义的数据和函数 并导出给template模板使用
    43. const { inputValue, handleInputChange } = useInputEffect();
    44. const { list, addItemToList } = useListEffect();
    45. const handleSubmit = () => {
    46. addItemToList(inputValue);
    47. };
    48. return {
    49. inputValue,
    50. handleInputChange,
    51. list,
    52. addItemToList,
    53. handleSubmit,
    54. };
    55. },
    56. template: `
    57. <div>
    58. <div>
    59. <el-input style="width:300px" :modelValue="inputValue" @input="handleInputChange"></el-input>
    60. <el-button type="primary" @click="handleSubmit">提交</el-button>
    61. </div>
    62. <div>
    63. <el-table
    64. border
    65. :data="list"
    66. style="width: 200px;">
    67. <el-table-column
    68. prop="content"
    69. label="待办事项"
    70. width="199">
    71. </el-table-column>
    72. </el-table>
    73. </div>
    74. </div>
    75. `,
    76. });
    77. app.use(ElementPlus);
    78. const vm = app.mount("#root");
    79. </script>
    80. </html>