0

I'm new to Vue and I'd like to make an AJAX call every time my component is rendered. I have a vue component lets say "test-table" and Id like to fetch the contents via an AJAX call. There are many such tables and I track the active one via an v-if/v-else-if etc.

Currently I have a cheaty solution: in the template for the component I call a computed property called getData via
{{ getData }}
which initiates the Ajax call but does only return an empty string. Id like to switch to the proper way but dont know how.

My code is like so: (its typescript)

Vue.component("test-table", {
        props: ["request"],
        data () {
            return {
                tableData: [] as Array<TableClass>,
            }
        },
        template: `{{ getData() }} DO SOME STUFF WITH tableData...`,
        computed: {
            getData() : string {
                get("./foo.php", this.request, true).then(
                    data => this.tableData = data.map(element => new TableClass(data))
                )
                return "";
            }
        }
}

HTML:

<test-table v-if="testcounter === 1" :request="stuff...">
<test-table v-else-if="testcounter === 2" :request="other stuff...">
...

get is an async method that just sends a GET request with request data to the server. The last parameter is only for saying the method to expect a JSON as answer. Similar to JQuerys getJSON method.

the "created" method does NOT work! It fires only one time when the component is first created. If I deactivate and activate again (with v-if) the method is not called again.

Btw: I'm using Vue 2.6.13

2
  • watch request, then fire getData, which should be a method not a computed prop Commented Jul 8, 2021 at 10:04
  • Wow, this could actually work. Why didn't u write an answer? Also do you know why they've downvoted my question? Isn't it specific enough or...? Commented Jul 8, 2021 at 14:06

3 Answers 3

1

Lifecycle hooks won't fire every time if the component is cached, keep-alive etc

Add a console.log in each of the lifecycle hooks to see.

Change to use a watcher which handles firing getData again if request changes.

  ...
  watch: {
    request: {
      handler: function() {
        this.getData()
      },
      deep: true
    }
  },
  created() {
     this.getData()
  },
  methods: {
    getData(): string {
      // do request
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

0

@FlorianBecker try the lifecycle hook updated(). It may be a better fit for what you're trying to achieve. Docs here.

1 Comment

This doesn't work since updated is only called, when a watched property changes and my properties only change on the Ajax call receive. Also it would be an infinite loop since every time the Ajax call comes back an update would be triggered cause it edited the property leading to an update which calles another Ajax call and so on and so forth
0

You should be able to use the mounted hook if your component is continuously rendered/unrendered using v-if, like so:

export default {
    mounted() {
        // do ajax call here
        this.callAMethod();
    },
    ...
}

Alternatively, you could use the created() hook but it is executed earlier in the chain, so this means the DOM template is not created yet so you cant refer to it. mounted usually is the way to go.

More info on these hooks can be found here.

2 Comments

Tried it, doesn't work. The mounted method is only called once the APP gets mounted. At least in my case. This doesn't server any purpose since it has to be updated constantly. But thx anyway
You probably have to assign a key="someidentifier" onto that test-table component such that it does not get re-used, which I believe is what's happening. Then, whenever the v-if triggers it to be shown, the component should trigger mounted again. But there are other roads towards a good solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.