vue3.2图片上传Demo搭建 3 搜索栏 实现

抽离form 表单    1通过defineEmits 实现  从上往下 传递 父子组件传递

<template>
   <form class="form-inline mt-2 mt-md-0">
       <input class="form-control"
        type="text" 
        placeholder="Search in Drive"
         aria-label="Search"
         v-model="q"
         @keydown.enter.prevent="handleEnterKey"
         />
     </form>

</template>
<script setup>
 import {ref} from 'vue';
 const q=ref("hello world")

 const emits=defineEmits(["handleValueQ"])

 const handleEnterKey=()=>{
        emits("handleValueQ",q.value)
 }
</script>

Navbar.vue

<template>
  <nav class="navbar navbar-light bg-white navbar-expand-lg border-bottom">
    <div class="container">
      <a class="navbar-brand" href="/"><strong>Vue</strong>图片上传</a>
         <SearchForm  @handleValueQ="sendQToApp($event)"/>
    </div>
  </nav>
</template>
<script setup>
import SearchForm from './SearchForm.vue'
const emits=defineEmits(["handleValueQ"])
const sendQToApp=(value)=>{
  console.log(value);
  emits("handleValueQ",value)
};

</script>

<style>
</style>

APP.vue



<template>
<Navbar  @handleValueQ="getValueFormSearchForm($event)"/>
<MyFiles  :q="q"/>

</template>

<script setup>
import  Navbar from './components/Navbar.vue'
import  MyFiles from './pages/MyFiles.vue'
import { ref } from 'vue';
const q= ref("");

const getValueFormSearchForm=(value)=>{
   q.value=value
console.log(value)

}

</script>

MyFiles.vue  

<template>
    <div class="container py-3">
        <ActionsBar />
        <div class="d-flex justify-content-between  align-items-center  py-2">
            <h6 class="text-muted mb-0">文件</h6>
           <SortToggler @sort-change="hanleSortChange($event)"/>
        </div>
        <FilesList :files="files"/>

    </div>

</template>
<script setup>
//http://localhost:3031/files?_sort=name&_order=desc 倒序
//http://localhost:3031/files?_sort=name&_order=asc 正序
import ActionsBar from '../components/ActionsBar.vue';
import FilesList from '../components/files/FilesList.vue';
import SortToggler from '../components/SortToggler.vue';
import axios from 'axios'

import { ref,onMounted,reactive,watchEffect,watch} from 'vue';
const props=defineProps({
  q:{
      type:String,
  },

})

const files=ref([]);
//修改query值 实现倒序正序
const query =reactive({
    _sort:"name",
    _order:"asc",
    q:"",

})
//平常我们通过查询字符串的方式传参需要通过对字符串进行处理后才能使用。
//URLSearchParams()中有帮我们做处理的方法直接返回处理后的数据

watchEffect(
   async ()=>{
       query.q=props.q;
   const {data}=await axios.get(
`http://localhost:3031/files?${new URLSearchParams(query)} `);

   files.value=data;
}
);

// watch(()=>query._order,
//    async ()=>{
//        console.log(`http://localhost:3031/files?${new URLSearchParams(query)} `)
//    const {data}=await axios.get(`http://localhost:3031/files?${new URLSearchParams(query)} `);
//     console.log(data)
//    files.value=data;
// },{immediate:true}
// );



const hanleSortChange =(payload)=>{
    console.log(payload);
 
    query._sort=payload.column;
    query._order=payload.order;  
}


</script>

<style>
</style>

2,SearchForm  从MyFiles 传值给SearchForm

* 1.要素从MyFiles传值给SearchForm

  * 2.桥梁NavBar 可以直接传值给SearchForm

  * 3.内容显示在navbar,数据的绑定放到MyFiles

通过   teleport  

<teleport to="#search-form">
                <SearchForm v-model="query.q"/>
        </teleport>
<template>
   <form class="form-inline mt-2 mt-md-0">
       <input class="form-control"
        type="text" 
        placeholder="Search in Drive"
         aria-label="Search"
         :value="modelValue"
         @keydown.enter.prevent="handleEnterKey"
         @keydown.esc.prevent="handleEscKey"
         />
     </form>

</template>
<script setup>
 import {ref} from 'vue';
 /**
  * 1.要素从MyFiles传值给SearchForm
  * 2.桥梁NavBar 可以直接传值给SearchForm
  * 3.内容显示在navbar,数据的绑定放到MyFiles
  */

const emits=defineEmits(["update:modelValue"]);


 const handleEnterKey=(event)=>{
     emits("update:modelValue",event.target.value);
 }

 const handleEscKey=()=>{
     emits("update:modelValue","");
 }

 defineProps({
    modelValue:{
        type:String,
    }
})

</script>
<template>
  <nav class="navbar navbar-light bg-white navbar-expand-lg border-bottom">
    <div class="container">
      <a class="navbar-brand" href="/"><strong>Vue</strong>图片上传</a>
       
         <div id="search-form"></div>
    </div>
  </nav>
</template>
<script setup>


</script>

<style>
</style>
<template>
    <div class="container py-3">
        <ActionsBar />
        <div class="d-flex justify-content-between  align-items-center  py-2">
            <h6 class="text-muted mb-0">文件</h6>
           <SortToggler @sort-change="hanleSortChange($event)"/>
        </div>
        <teleport to="#search-form">
                <SearchForm v-model="query.q"/>
        </teleport>
        <FilesList :files="files"/>

    </div>

</template>
<script setup>
//http://localhost:3031/files?_sort=name&_order=desc 倒序
//http://localhost:3031/files?_sort=name&_order=asc 正序
import SearchForm from '../components/SearchForm.vue';
import ActionsBar from '../components/ActionsBar.vue';
import FilesList from '../components/files/FilesList.vue';
import SortToggler from '../components/SortToggler.vue';
import axios from 'axios'

import { ref,onMounted,reactive,watchEffect,watch} from 'vue';


const files=ref([]);
//修改query值 实现倒序正序
const query =reactive({
    _sort:"name",
    _order:"asc",
     q:"",
})
//平常我们通过查询字符串的方式传参需要通过对字符串进行处理后才能使用。
//URLSearchParams()中有帮我们做处理的方法直接返回处理后的数据

watchEffect(
   async ()=>{
    //    query.q=props.q;
   const {data}=await axios.get(
`http://localhost:3031/files?${new URLSearchParams(query)} `);

   files.value=data;
}
);

// watch(()=>query._order,
//    async ()=>{
//        console.log(`http://localhost:3031/files?${new URLSearchParams(query)} `)
//    const {data}=await axios.get(`http://localhost:3031/files?${new URLSearchParams(query)} `);
//     console.log(data)
//    files.value=data;
// },{immediate:true}
// );



const hanleSortChange =(payload)=>{
    console.log(payload);
 
    query._sort=payload.column;
    query._order=payload.order;  
}


</script>

<style>
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值