1

How to can import turn.js to nuxt.js project. When I use through plugin then error Window not define, even though I used it in client mode

  • Update: I used required package check is client, but I get error jquery__WEBPACK_IMPORTED_MODULE_3___default(...)(...).turn is not a function
<template>
    <div id="flipbook">
        <div class="hard">
            Turn.js
        </div>
        <div class="hard" />
        <div> Page 1 </div>
        <div> Page 2 </div>
        <div> Page 3 </div>
        <div> Page 4 </div>
        <div class="hard" />
        <div class="hard" />
    </div>
</template>

<script>
    import $ from 'jquery';

    if (process.client) {
        require('turn.js');
    }

    export default {
        mounted() {
            console.log($('#flipbook').turn('page'));
            $('#flipbook'.turn({
                width: 400,
                height: 300,
                autoCenter: true,
            }));
        },

    };
</script>

kissu
  • 40,416
  • 14
  • 65
  • 133
Stone0409
  • 33
  • 1
  • 6

1 Answers1

1

EDIT + TLDR: use flipbook-vue because it is better on any point really (weight, maintenance, flexibility, doesn't rely on jQuery etc...).
This is also working perfectly fine with Nuxt as shown here.


If you need to install jQuery into your Nuxt project (which I do not recommend), you can follow my answer here. Still, an alternative without jQuery would be greatly appreciated IMO, even more if you're using it just for selecting an element in the DOM. Is it worth 20kb?

If you want to target a specific element available into your component, please use the $refs syntax, as explained in this other answer. And not a usual querySelector.

require is node-based, you should rather use import since you're in a modern browser based ecosystem when using Vue. Hence, import your package locally with a dynamic import is still the best way to go here.

TLDR: something like this is usually enough to make it work

<template>
  <div id="flipbook" ref="flipbook">
    <div class="hard">
      Turn.js
    </div>
    <div class="hard" />
    <div> Page 1 </div>
    <div> Page 2 </div>
    <div> Page 3 </div>
    <div> Page 4 </div>
    <div class="hard" />
    <div class="hard" />
  </div>
</template>

<script>
export default {
  async mounted () {
    if (process.client) {
      const turnJs = await import('turn.js')
      this.$refs.flipbook.turnJs.turn({
        width: 400,
        height: 300,
        autoCenter: true,
      })
    }
  }
}
</script>

Meanwhile, the project did not get any updates for already 10 years so I guess that you can pass on this one.

enter image description here

Writing your own with a bit of CSS + JS is also totally feasible and will provide the best results IMO.

tony19
  • 125,647
  • 18
  • 229
  • 307
kissu
  • 40,416
  • 14
  • 65
  • 133
  • I have a question. what is ``` this.flipbook.turnJs.turn({ width: 400, height: 300, autoCenter: true, }) ``` – Stone0409 Nov 07 '21 at 18:41
  • @Stone0409 sorry, as you have guessed, it was missing the `$refs` here. – kissu Nov 07 '21 at 18:43
  • I tried, but have error (Cannot read properties of undefined (reading 'turn')) :(( – Stone0409 Nov 07 '21 at 18:45
  • @Stone0409 yeah, I'm not sure of the exact syntax it may be using since it's not a maintained package as written above. The `import` syntax may just not be supported. Do you have something inside of `turnJs`? – kissu Nov 07 '21 at 18:47
  • oh yep, nothing is inside turnJs :(((( – Stone0409 Nov 07 '21 at 18:49
  • @Stone0409 alright, since the [documentation is 404](https://github.com/blasten/turn.js/wiki/Reference), not maintained and since the module only works with `require`, I **HIGHLY** suggest you not using this library. Because loading `jQuery + turnjs` will be around 40kB (if not depending on something else), while Vue is only 23kB. So I do hardly recommend passing on this one and try to find something more modern or do it yourself. Loading 70kB or JS only for a page scroll is a nogo IMO. – kissu Nov 07 '21 at 18:59
  • @Stone0409 use this one: https://github.com/ts1/flipbook-vue It is maintained, compatible with Vue, weights 7kB, seems configurable and more pretty IMO. – kissu Nov 07 '21 at 19:02
  • i tried, but flipbook don't config inside page :((( – Stone0409 Nov 07 '21 at 19:03
  • @Stone0409 where did you tried? Cannot see it. – kissu Nov 07 '21 at 19:04
  • @Stone0409 this one seems to be working perfectly fine: https://github.com/ts1/flipbook-vue/issues/2#issuecomment-513428916 – kissu Nov 07 '21 at 19:06
  • i tried before, but i can't configure the content in each page – Stone0409 Nov 07 '21 at 19:06
  • @Stone0409 I'm not sure what you're talking about when saying `in each page`. Nuxt is (at least) an SPA at the end of the day. Could you please give us more details? – kissu Nov 07 '21 at 19:57