移动端 - 搜索组件(search-input篇)

移动端 - 搜索组件(suggest篇)

移动端 - 搜索组件(search-list篇)

我们先来看一下最终效果

 


这样的搜索组件在移动端是很常见的, 大部分需求都是:

1. 搜索框进行搜索关键字

2.  热门搜索

3. 搜索历史

4. 搜索结果(提供上拉加载效果)


上述的基本需求也是我们现在需要去实现的, 先来说一下大致的方向:

1. search 一般都是一个路由组件, 所以先创建 search.vue 组件

2. 拆分 search-input (搜索框业务组件), 实现其需求

3. 拆分 search-suggest (搜索结果业务组件), 实现其需求

4. 拆分 search-list (搜索历史基础组件), 持久化保存用户搜索数据

5. 在 search.vue 父组件中使用上述拆分出去的组件, 根据条件动态渲染热门搜索, 搜索历史或搜索结果组件


第一步:

1. 抽离 search-input.vue 组件

抽离的原因就是为了进行复用, 可能在项目的其他地方会使用到; 这也是 vue 的特色之一

<template>
  <div class="search-input">
    <!-- 搜索图标 -->
    <i class="icon-search"></i>
      
    <!-- input框 -->
    <input class="input-inner"/>
      
    <!-- 清空搜索数据图标 -->
    <i class="icon-dismiss"></i>
  </div>
</template>

<script>
export default {
  name: 'search-input'
}
</script>

<style lang="scss" scoped>
.search-input {
  display: flex;
  align-items: center;
  box-sizing: border-box;
  width: 100%;
  padding: 0 6px;
  height: 32px;
  background: $color-highlight-background;
  border-radius: 6px;
  .icon-search {
    font-size: 24px;
    color: $color-text-d;
  }
  .input-inner {
    flex: 1;
    margin: 0 5px;
    line-height: 18px;
    background: $color-highlight-background;
    color: $color-text;
    font-size: $font-size-medium;
    outline: 0;
    &::placeholder {
      color: $color-text-d;
    }
  }
  .icon-dismiss {
    font-size: 16px;
    color: $color-text-d;
  }
}
</style>

先来说一下, 这一个组件需要得到什么数据; 返回给父组件什么数据:

1. 返回出去用户输入的关键字信息(父组件拿到发送请求获取数据)

2. 接收到父组件传入的关键字信息, 然后显示到 input 框中(父组件主动修改的情况)

首先完成第一个需求:

1. 在 search-input 组件中定义 v-model 数据

2. watch 监听对应数据的变化, 一旦变化 emit 修改父组件数据(当然需要进行节流操作)

<template>
  <div class="search-input">
    <i class="icon-search"></i>
    <input class="input-inner" v-model="query"/>
    <i class="icon-dismiss"></i>
  </div>
</template>

<script>
export default {
  name: 'search-input',
  props: {
    modelValue: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      query: this.modelValue
    }
  },
  watch: {
    query (newQuery) {
      this.$emit('update:modelValue', newQuery.trim())
    }
  }
}
</script>

<style lang="scss" scoped>
...
</style>

因为监听 query 数据变化的时候, 会用到 debounce 方法; 所以 watch 的监听需要做一些改变

<script>
import { debounce } from 'throttle-debounce'

created () {
    this.$watch('query', debounce(300, (newQuery) => {
    	this.$emit('update:modelValue', newQuery.trim())
    }))
}

// watch: {
//   query: debounce(3000, function (newQuery) {
//     this.$emit('update:modelValue', newQuery.trim())
//   })
// }
</script>

再来完成第二个需求:

1. 在父组件中定义 v-model 数据

<template>
  <div class="search">
    <div class="search-input-wrapper">
        
      <!-- 搜索框组件 -->
      <SearchInput v-model="query"></SearchInput>
    </div>
  </div>
</template>

<script>
import SearchInput from '@/components/search/search-input'
import { ref } from 'vue'
export default {
  name: 'searchCom',
  components: {
    SearchInput
  },
  setup () {
    const query = ref('')

    return { query }
  }
}

现在整体的父子组件交互需求是完成了, 我们还需要去完善一下其他的需求

1. 点击清空数据按钮, 清空数据

2. 扩展 input 框的 placeholder 属性

<template>
  <div class="search-input">
    <i class="icon-search"></i>
    <input class="input-inner" v-model="query" :placeholder="placeholder" />
    <!-- 只有当query有值是才会显示 -->
    <i class="icon-dismiss" v-show="query" @click="clear"></i>
  </div>
</template>

<script>
    props: {
        modelValue: {
            type: String,
            default: ''
        },
        placeholder: {
            type: String,
            default: '搜索歌曲、歌手'
        }
    },
    methods: {
        clear () {
            this.query = ''
        }
    }
</script>

现在父组件中, search-input 组件封装完毕了; 然后还要在父组件中显示 "热门搜索" 的需求

1. 首先热门搜索的数据是发送请求获取来的(响应式变量进行接收)

2. 拿着数据对模板进行 v-for 遍历

3. 给每一个元素添加点击事件, 触发点击事件时; 会修改掉 query 的数据

<template>
  <div class="search">

    <!-- 搜索组件 -->
    <div class="search-input-wrapper">
      <SearchInput v-model="query"></SearchInput>
    </div>

    <!-- 热门搜索 -->
    <div class="search-content">
      <div class="hot-keys">
        <h1 class="title">热门搜索</h1>
        <ul>
          <li
            class="item"
            v-for="item in hotKeys"
            :key="item.id"
            @click="addQuery(item.key)"
          >
            <span>{{item.key}}</span>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { getHotKeys } from '@/api/search'

getHotKeys().then(res => {
	hotKeys.value = res.hotKeys
})

// 点击数据修改子组件数据
const addQuery = (key) => {
	query.value = key
}
</script>

<think>嗯,用户想知道如何在Ubuntu中启用和配置root用户登录。我之前记得Ubuntu默认是禁用root用户的,需要通过sudo来执行管理员命令。那具体步骤应该是什么呢? 首先,根据引用里的资料,启用root用户需要使用sudo passwd root命令来设置密码。对的,用户需要先创建一个普通用户,这个用户在安装时会被加入admin组,然后才有权限启用root。设置密码后,可以用su root切换,输入新密码就能进入root账户了。不过用户可能还想让root通过SSH登录,这需要修改SSH的配置。 然后,查看引用里的步骤,比如引用[3]和引用[5]提到要编辑/etc/ssh/sshd_config文件,找到PermitRootLogin这一项,改成yes。可能还需要开启密码认证,把PasswordAuthentication设为yes。改完后重启SSH服务,比如用systemctl restart sshd或者service ssh restart。这样root就能远程登录了。 不过用户可能会有疑问,比如为什么要先设置root密码?因为默认情况下root没有密码,所以必须先设置。另外,修改SSH配置的时候要注意权限,可能需要用sudo来编辑文件。还有安全性问题,启用root远程登录可能会有风险,建议用户考虑使用密钥认证或者限制访问IP。 用户可能还会遇到问题,比如修改配置后无法登录,这时候要检查防火墙设置,或者确认SSH服务正确重启。另外,如果PermitRootLogin被注释了,需要取消注释或者新增一行,确保配置生效。 总的来说,步骤应该是:1. 设置root密码;2. 切换root测试;3. 修改SSH配置允许root登录;4. 启用密码认证;5. 重启服务。同时提醒用户注意安全风险,建议仅在必要时启用,并采取其他安全措施。</think>### Ubuntu启用和配置root用户登录的详细步骤 #### 一、启用root用户 1. **设置root密码** 在终端执行命令: ```bash sudo passwd root ``` 输入当前用户密码(安装系统时创建的账户密码),接着设置新的root密码并确认[^2][^3][^5]。 2. **验证root切换** 输入命令切换到root用户: ```bash su root ``` 输入新设置的root密码后,命令行提示符将变为`root@hostname:#`,表示已进入root权限环境[^1][^2]。 3. **退出root用户** 在root权限下输入: ```bash exit ``` 可返回普通用户模式[^1]。 --- #### 二、配置SSH允许root登录(可选) > *注意:此操作可能降低系统安全性,建议仅在受信任环境中使用* 1. **修改SSH配置文件** 执行命令编辑配置文件: ```bash sudo nano /etc/ssh/sshd_config ``` 找到以下两处配置项: - 将 `#PermitRootLogin prohibit-password` 改为 `PermitRootLogin yes` - 将 `PasswordAuthentication no` 改为 `PasswordAuthentication yes`[^4] 2. **重启SSH服务** 执行命令使配置生效: ```bash sudo systemctl restart sshd # 或使用旧版命令 sudo service ssh restart ``` --- #### 三、验证远程登录 使用SSH客户端连接: ```bash ssh root@服务器IP ``` 输入root密码后若成功登录,说明配置生效[^3]。 --- ### 安全建议 1. **最小化启用原则** 仅在必要时启用root远程登录,日常操作建议通过`sudo`提权。 2. **密钥认证替代密码** 通过配置SSH密钥登录可大幅提升安全性,操作步骤: ```bash # 生成密钥对 ssh-keygen -t rsa # 上传公钥到服务器 ssh-copy-id root@服务器IP ``` 3. **防火墙限制** 使用UFW限制SSH访问IP: ```bash sudo ufw allow from 指定IP to any port 22 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值