最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calling service method in a Vue template - Stack Overflow

programmeradmin4浏览0评论

I have

<template>
  <button @click="doSomething">...</button>
</template>

<script>
import ApiService from '../service/api.service';
export default {
  setup() {
      function doSomething() {
        ApiService.doSomething();
      }
      return {
        doSomething
      }
  }
}
</script>

If I make it shorter (like I'm used in Angular):

<template>
  <button @click="ApiService.doSomething">...</button>
</template>

<script>
import ApiService from '../service/api.service';
</script>

It doesn't work, throws a runtime error. (It doesn't work even if I try as function call: @click="ApiService.doSomething()"

Can you help, how to save time/code and directly call ApiService.doSomething just in a template?


P.S. they asked me to post ApiService also so here it is: api.service.js:

import axios from 'axios';
import router from '@/router';

const ApiService = {

    init(baseURL) {
        axios.defaults.baseURL = baseURL;
    },

    //...

    doSomething() {
        router.push({ path: '/goSomewhere' });
    },

}

export default ApiService

I have

<template>
  <button @click="doSomething">...</button>
</template>

<script>
import ApiService from '../service/api.service';
export default {
  setup() {
      function doSomething() {
        ApiService.doSomething();
      }
      return {
        doSomething
      }
  }
}
</script>

If I make it shorter (like I'm used in Angular):

<template>
  <button @click="ApiService.doSomething">...</button>
</template>

<script>
import ApiService from '../service/api.service';
</script>

It doesn't work, throws a runtime error. (It doesn't work even if I try as function call: @click="ApiService.doSomething()"

Can you help, how to save time/code and directly call ApiService.doSomething just in a template?


P.S. they asked me to post ApiService also so here it is: api.service.js:

import axios from 'axios';
import router from '@/router';

const ApiService = {

    init(baseURL) {
        axios.defaults.baseURL = baseURL;
    },

    //...

    doSomething() {
        router.push({ path: '/goSomewhere' });
    },

}

export default ApiService
Share Improve this question edited Nov 16, 2020 at 17:37 Dalibor asked Nov 16, 2020 at 16:29 DaliborDalibor 1,5721 gold badge20 silver badges49 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

It doesn't work because you need to pass it through the setup function to be available in the template.

you can do this though:

<template>
  <button @click="ApiService.doSomething">...</button>
</template>

<script>
import ApiService from '../service/api.service';
export default {
  setup() {
      return { ApiService }
  }
}
</script>

Alternatively, you may also use the setup attribute for script, which does some hoisting magic for you. (rfc link)

<template>
  <button @click="ApiService.doSomething">...</button>
</template>

<script setup>
import ApiService from '../service/api.service';
export ApiService;
</script>

You could spread that service functions in the methods option methods:{...ApiService } :

<template>
  <button @click="doSomething">...</button>
</template>

<script>
import  ApiService from '../service/api.service';
export default {
methods:{
  ...ApiService
}
}
</script>

api.service.js

const ApiService = {
  doSomething() {
    console.log("doing something");
  },

  doSomething2() {
    console.log("doing something 2");
  },

  doSomething3() {
    console.log("doing something 3");
  }
};

export default ApiService;

LIVE DEMO

Could you try adding parentheses to the function call?

<template>
  <button @click="ApiService.doSomething()">...</button>
</template>

<script>
import ApiService from '../service/api.service';
</script>
发布评论

评论列表(0)

  1. 暂无评论