【Vue.js】基础知识 知识点

1、过滤器
例子

<div id="app">
  {
     {
      message | capitalize }}
</div>
<script>
new Vue({
     
  el: '#app',
  data: {
     
    message: 'runoob'
  },
  filters: {
     
    capitalize: function (value) {
     
      if (!value) return ''
      value = value.toString()
      return value.charAt(0).toUpperCase() + value.slice(1)
    }
  }
})
</script>

过滤器串联

{
     {
      message | filterA | filterB }}

过滤器接收参数

{
     {
      message | filterA('arg1', arg2) }}

这里,message 是第一个参数,字符串 ‘arg1’ 将传给过滤器作为第二个参数, arg2 表达式的值将被求值然后传给过滤器作为第三个参数。

2、计算属性使用模板

<div id="app">
  <p>原始字符串: {
     {
      message }}</p>
  <p>计算后反转字符串: {
     {
      reversedMessage }}</p>
</div>

<script>
var vm = new Vue({
     
  el: '#app',
  data: {
     
    message: 'Runoob!'
  },
  computed: {
     
    // 计算属性的 getter
    reversedMessage: function () {
     
      // `this` 指向 vm 实例
      return this.message.split('').reverse().join('')
    }
  }
})
</script>

3、methods和computed的区别
我们可以使用 methods 来替代 computed,效果上两个都是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时
才会重新取值。而使用 methods ,在重新渲染的时候,函数总会重新调用执行。可以说使用 computed 性能会更好,但是如果你不希
望缓存,你可以使用 methods 属性。

4、 ', data: function () { return { counter: 0 } }, methods: { incrementHandler: function () { this.counter += 1 this.$emit('increment') } }, }) new Vue({ el: '#counter-event-example', data: { total: 0 }, methods: { incrementTotal: function () { this.total += 1 } } }) </script>

14、路由

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
 
<div id="app">
  <h1>Hello App!</h1>
  <p>
    <!-- 使用 router-link 组件来导航. -->
    <!-- 通过传入 `to` 属性指定链接. -->
    <!-- <router-link> 默认会被渲染成一个 `` 标签 -->
    <router-link to="/foo">Go to Foo</router-link>
    <router-link to="/bar">Go to Bar</router-link>
  </p>
  <!-- 路由出口 -->
  <!-- 路由匹配到的组件将渲染在这里 -->
  <router-view></router-view>
</div>
<script>
// 0. 如果使用模块化机制编程,导入 Vue 和 VueRouter,要调用 Vue.use(VueRouter)
 
// 1. 定义(路由)组件。
// 可以从其他文件 import 进来
const Foo = {
      template: '
foo
'
} const Bar = { template: '
bar
'
} // 2. 定义路由 // 每个路由应该映射一个组件。 其中"component" 可以是 // 通过 Vue.extend() 创建的组件构造器, // 或者,只是一个组件配置对象。 // 我们晚点再讨论嵌套路由。 const routes = [ { path: '/foo', component: Foo }, { path: '/bar', component: Bar } ] // 3. 创建 router 实例,然后传 `routes` 配置 // 你还可以传别的配置参数, 不过先这么简单着吧。 const router = new VueRouter({ routes // (缩写)相当于 routes: routes }) // 4. 创建和挂载根实例。 // 记得要通过 router 配置参数注入路由, // 从而让整个应用都有路由功能 const app = new Vue({ router }).$mount('#app') // 现在,应用已经启动了! </script>

15、ajax(axios)
get实例

new Vue({
     
  el: '#app',
  data () {
     
    return {
     
      info: null
    }
  },
  mounted () {
     
    axios
      .get('https://www.runoob.com/try/ajax/json_demo.json')
      .then(response => (this.info = response))
      .catch(function (error) {
      // 请求失败处理
        console.log(error);
      });
  }
})

get传参

// 直接在 URL 上添加参数 ID=12345
axios.get('/user?ID=12345')
  .then(function (response) {
     
    console.log(response);
  })
  .catch(function (error) {
     
    console.log(error);
  });
 
// 也可以通过 params 设置参数:
axios.get('/user', {
     
    params: {
     
      ID: 12345
    }
  })
  .then(function (response) {
     
    console.log(response);
  })
  .catch(function (error) {
     
    console.log(error);
  });

post实例

new Vue({
     
  el: '#app',
  data () {
     
    return {
     
      info: null
    }
  },
  mounted () {
     
    axios
      .post('https://www.runoob.com/try/ajax/demo_axios_post.php')
      .then(response => (this.info = response))
      .catch(function (error) {
      // 请求失败处理
        console.log(error);
      });
  }
})

post传参

axios.post('/user', {
     
    firstName: 'Fred',        // 参数 firstName
    lastName: 'Flintstone'    // 参数 lastName
  })
  .then(function (response) {
     
    console.log(response);
  })
  .catch(function (error) {
     
    console.log(error);
  });

执行多个并发请求

function getUserAccount() {
     
  return axios.get('/user/12345');
}
 
function getUserPermissions() {
     
  return axios.get('/user/12345/permissions');
}
axios.all([getUserAccount(), getUserPermissions()])
  .then(axios.spread(function (acct, perms) {
     
    // 两个请求现在都执行完成
  }));

配置、拦截等见菜鸟教程https://www.runoob.com/vue2/vuejs-ajax-axios.html

你可能感兴趣的:(js,vue,css,vue.js,javascript)